How to search for a substring in an array of strings in node js and also add the matching elements to another array?

For example :

var arr = ["accounts","accountable","address","abandon","ahead"];

Suppose,I want to search for words starting with 'acc' and add those words to another array.
I tried using startsWith() but its not working.

1 1 566
1 REPLY 1

Try this


var arr = ["accounts","accountable","address","abandon","ahead"];

const result = arr.filter(word => word.startsWith('acc'));

var array1 = ['a', 'b', 'c'];

console.log(array1.concat(result));