0

Lets say I have array:-

data = ['abc abc abc', 'def', 'ghi','abc','abc e']

and I want to filter all the value from array which have 'abc'.

my expected output is newArray = ['abc abc abc','abc','abc e']

So I tried:-

var newArray = data.map(item=>({ 
    item.split(" ").filter(function (getValue) {
  return getValue =='abc';
})
}
))

But its giving me error.

How can I do that? Thanks!!!

Shahanshah Alam
  • 565
  • 7
  • 22

3 Answers3

1

You can use array#filter with String.prototype.includes().

const data = ['abc abc abc', 'def', 'ghi','abc','abc e'],
      result = data.filter(w => w.includes('abc'));
console.log(result);
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
1

Alternative:

let data = ['abc abc abc', 'def', 'ghi','abc','abc e']


let myArr = data.filter(element => element.match(/abc/g))

console.log(myArr)
Aks Jacoves
  • 849
  • 5
  • 9
1

You can make use of .includes array method to achieve the same

var data = ['abc abc abc', 'def', 'ghi','abc','abc e'];

var result = data.filter(obj => obj.includes('abc'));

console.log(result);
Harmandeep Singh Kalsi
  • 3,315
  • 2
  • 14
  • 26