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

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -