javascript - Compare array js -
this question has answer here:
- javascript array difference 53 answers
i have following
var = [4,6,12]; var b = [4,6]; (var = 0; < a.length; i++) { (var j = 0; j < b.length; j++) { if (a[i] !== b[j]) { a.pop(); } } }
i want compare 2 , remove 12 if not found in b. not want create new array result remove a.
however, if console log empty.
the reason why a
coming out empty because of double loop. you're comparing each element of a
every other element of b
. when hit a[0] == b[1], obviously, 4 != 6 pop it.
Comments
Post a Comment