javascript - Compare 2 arrays and show unmatched elements from array 1 -
this question has answer here:
- javascript array difference 53 answers
i have 2 arrays follows. want compare both arrays , provide elements 'check' not present in 'data' array.
var check= ["044", "451"], data = ["343", "333", "044", "123", "444", "555"]; the function used follows. function result in providing elements in 'check' array present in 'data' array
function getmatch(a, b) { var matches = []; ( var = 0; < a.length; i++ ) { ( var e = 0; e < b.length; e++ ) { if ( a[i] === b[e] ) matches.push( a[i] ); } } return matches; } getmatch(check, data); // ["044"] ---> answer '044' present in 'data' i want have list of elements not present in 'data' array. can let me know how achieve this.
you use filter , set, providing set context filter method, can accessed this:
var check= ["044", "451"], data = ["343", "333", "044", "123", "444", "555"]; var res = check.filter( function(n) { return !this.has(n) }, new set(data) ); console.log(res); note runs in o(n) time, contrary indexof/includes based solutions, represent nested loop.
Comments
Post a Comment