Answer the question
In order to leave comments, you need to log in
How to check 2 arrays and remove the same values?
Suppose there is an array there is an array
a = {"a": "17","b": "1","d": "3","v": "10","e": "4","f": "9"}
and there is a second array b = {"a": "1","b": "3","d": "4","v": "5","e": "6","f": "7"}
Answer the question
In order to leave comments, you need to log in
How how? Handles! Only these are not arrays, but objects with fields.
var a = {"a": "17","b": "1","d": "3","v": "10","e": "4","f": "9"};
var b = {"a": "1", "b": "3","d": "4","v": "5", "e": "6","f": "7"};
function search(a, b) {
var result = [];
for(var akey in a) {
var found = false;
for(var bkey in b) {
if (a[akey] == b[bkey]) {
found = true;
continue;
}
}
if (!found) {
result.push(a[akey]);
}
}
return result;
}
console.log(search(a, b));
var a = [17, 1, 3, 0, 4, 9];
var b = [1, 3, 4, 5, 6, 7];
function search(a, b) {
var result = [];
for(var i = 0; i < a.length; i++) {
if (b.indexOf(a[i]) == -1) {
result.push(a[i]);
}
}
return result;
}
console.log(search(a, b));
console.log(search(b, a));
var a = [17, 1, 3, 0, 4, 9];
var b = [1, 3, 4, 5, 6, 7];
function search(a, b) {
var result = [];
a.forEach(function(v) {
if (b.indexOf(v) == -1) {
result.push(v);
}
});
return result;
}
console.log(search(a, b));
console.log(search(b, a));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question