I'm having some issues comparing the elements of two arrays and filtering out matching values. I only want to return array elements that are NOT included within wordsToRemove
.
var fullWordList = ['1','2','3','4','5'];
var wordsToRemove = ['1','2','3'];
var filteredKeywords = fullWordList.forEach(function(fullWordListValue) {
wordsToRemove.filter(function(wordsToRemoveValue) {
return fullWordListValue !== wordsToRemoveValue
})
});
console.log(filteredKeywords);
JavaScript finding non-matching values in two arrays The code will look like this. const array1 = [1, 2, 3, 4, 5, 6]; const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]; const output = array2. filter(function (obj) { return array1. indexOf(obj) === -1; }); console.
Check if two arrays are equal or not using Sorting Follow the steps below to solve the problem using this approach: Sort both the arrays. Then linearly compare elements of both the arrays. If all are equal then return true, else return false.
You can use filter
and includes
to achieve this:
var fullWordList = ['1','2','3','4','5'];
var wordsToRemove = ['1','2','3'];
var filteredKeywords = fullWordList.filter((word) => !wordsToRemove.includes(word));
console.log(filteredKeywords);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With