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!

4
  • Includes works in ES6. How are you compiling Commented Apr 29, 2020 at 15:19
  • Have you seen this stackoverflow.com/questions/1584370/… Commented Apr 29, 2020 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. Commented Apr 29, 2020 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. Commented Apr 29, 2020 at 15:25

2 Answers 2

1

Try this as an alternative:

const res= arr1.filter(x => arr2.indexOf(x.toString()) === -1)
Sign up to request clarification or add additional context in comments.

1 Comment

indexOf was expecting string but x was of the type {}
1

You can try.

var arr3 = arr1.filter(x => arr2.indexOf(x)<0);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.