Skip to content
Advertisement

compare two arrays and remove partial match in javascript

const arr1=['hello from the other side', 'very nice boy,john']
const arr2=['nice', 'work']

output = ['work']

I am stuck on this problem idk the right code for it in nodejs. when I do string to word it works and when I do word to string it doesn’t work. how can I filter the word array by matching the sentence array?

var cleanArray = c.filter(element => c.every(item => !element.includes(item)));

This is my current code and it works only from sentence to word and not word to sentence

Advertisement

Answer

You should use some instead of every.

The some() method tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array. – MDN

const arr1 = ["hello from the other side", "very nice boy,john"];
const arr2 = ["nice", "work"];

const result = arr2.filter((s) => !arr1.some((str) => str.includes(s)));
console.log(result);
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement