javascript - Most effective and elegant way to find matching Strings in two arrays in Node.js -
this question has answer here:
i have 2 string arrays:
var x = ['a', 'b', 'c'], y = ['b', 'c', 'd']; i need create array z, such z = ∩ b
z = ['b', 'c']; z contains strings both in x , y.
i've done way:
a.foreach(function(i) { b.foreach(function(j) { if (i === j) z.push(i); }); }); any suggestions / comments?
i'd better use filter method:
['a', 'b', 'c'].filter(function(c) { return ['b', 'c', 'd'].indexof(c) > -1; });
Comments
Post a Comment