javascript check existence of elements of 1 array inside the other -
for searching elements of 1 array inside other, 1 may use indexof() on target of search , run loop on other array elements , in each step check existence. trivial , knowledge on javascript too. 1 please suggest more efficient way? maybe built-in method of language help? although couldn't hit such method using google.
you can use array.filter() internally , implement function on array's prototype returns elements common both.
array.prototype.common = function(a) { return this.filter(function(i) { return a.indexof(i) >= 0; }); }; alert([1,2,3,4,5].common([4,5,6])); // "4, 5"
again mention in post, logic works taking each element , checking whether exists in other.
Comments
Post a Comment