javascript - Elegant method to compare an array of strings to another array of strings -
question: how can elegantly compare array of strings array of strings returning array of non-matching strings
var master = ['1','2','3','4'] var versioned = ['1a','2','3b','4'] var errorlog = [] var count = 0; //this loop doesn't work :( for(var = 0; < versioned.length - 1; ++i ){ for(var j = 0; j < master.length -1; ++j){ if(versioned[i] === master[j]){ console.log('cleared'); } if(count === master.length){ errorlog.push(versioned[i]); } } }
loop return ['1a', '3b'];
i feel filter()
or map()
or reduce()
i'm unable wrap brain around properly.
var master = ['1','2','3','4']; var versioned = ['1a','2','3b','4']; function diff(needle, haystack){ return needle.filter(function(item){ return !~haystack.indexof(item); }); } console.log(diff(versioned, master)); //["1a", "3b"];
~
noting number equals -(x + 1). ~-1 becomes 0, falsy.
~master.indexof(item)
same master.indexof(item) !== -1
Comments
Post a Comment