0
var arr1= ['test', 'test1', 'test2', 'test3'];

var arr2= ['test', 'test1'];

expected output ['test2','test3'] I am able to achieve this in the browser console using -

arr1.filter(x => !arr2.includes(x));

But the problem is that "includes" seems to be not supporting in ES6.

Is there an alternative? Thanks!

Sallu
  • 116
  • 7
  • Includes works in ES6. How are you compiling – Batman Apr 29 '20 at 15:19
  • Have you seen this https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items – Hides Apr 29 '20 at 15:21
  • Yeah, but it always merges from both arrays. I need content only from arr1 but removing entries which is present in arr2. – Sallu Apr 29 '20 at 15:24
  • It gives me this error TS2339: Property 'includes' does not exist on type 'string[]' when I try generating bundle in react. After searching for fix I read that ES6 does not support this. – Sallu Apr 29 '20 at 15:25

2 Answers2

1

Try this as an alternative:

const res= arr1.filter(x => arr2.indexOf(x.toString()) === -1)
Sallu
  • 116
  • 7
Hadi Samadzad
  • 1,480
  • 2
  • 13
  • 22
1

You can try.

var arr3 = arr1.filter(x => arr2.indexOf(x)<0);
Srijan
  • 100
  • 1
  • 6