Skip to content

Instantly share code, notes, and snippets.

@S-codes14
Created May 30, 2022 09:54
Show Gist options
  • Save S-codes14/9a1d9baf1d4a1169285ece66bda273d8 to your computer and use it in GitHub Desktop.
Save S-codes14/9a1d9baf1d4a1169285ece66bda273d8 to your computer and use it in GitHub Desktop.
How to compare two arrays in javascript and then output return array of missing
var ArrayFileName = ['one', 'two', 'three', 'three', 'five', 'six', 'ten'];
var ArrayFileName2 = ['one', 'two', 'three', 'ten', 'eleven'];
var final = ArrayFileName2.filter(function(item) {
for (var i = 0; i < ArrayFileName.length; i++) {
if (ArrayFileName[i] === item) return false;
}
return true;
})
console.log(final); // ['eleven']
var final2 = ArrayFileName.filter(function(item) {
for (var i = 0; i < ArrayFileName2.length; i++) {
if (ArrayFileName2[i] === item) return false;
}
return true;
})
console.log(final2) //['five', 'six']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment