Intersection between two arrays in javascript

Votes : 5
3646

There are few methods to get intersection between two array in javascript.

<script>Array.prototype.intersect1 = function (e) { /* * Get intersection between two array * * @param (array|required) e - array * @return (array) Intersection between two array */ e = e || []; return this.filter(function(i) {return e.indexOf(i) > 0;}); }</script>

This method is not complex one. There is used javascript filter() method to get values one by one from first array and find out witch values have been already exist on second array. Return first array values, witch are include in second array.

Example:

console.log([1,2,3].intersect1([2,3,4,5]));output:/ [2, 3]

console.log([1,2,3].intersect1([2,3,3,4,5]));output:/ [2, 3]

console.log([1,2,2,3].intersect1([2,3,4,5]));output:/ [2, 2, 3]

console.log([1,2,3,3].intersect1([2,3,4,5]));output:/ [2, 3, 3]

console.log([1,4,3,2].intersect1([2,3,4,5]).sort());output:/ [2, 3, 4]

<script>if (!Array.prototype.intersect2){ /* * Get intersection between two array * * @param (array|required) e - array * @return (array) Intersection between two array */ Array.prototype.intersect2 = function (e) { var tmp = [],lst = {},args = this || []; e = e || []; for(i in e){ switch(typeof e[i]){ case 'function': break; default: lst[e[i]] = 1; break; } } for(i in args) { switch (typeof args[i]) { case 'function': break; default: if(lst.hasOwnProperty(args[i]) && tmp.indexOf(args[i]) == -1){ tmp.push(args[i]); } break; } } return tmp; }; }</script>

The method specially design to use to get intersection from between two arrays and also objects. If there will be something wrong about your given parameters to get intersection. This function will be already ignored issue and then return intersection.

Example:

console.log([1,2,3].intersect2([2,3,4,5]));output:/ [2, 3]

console.log([1,2,3].intersect2([2,3,3,4,5]));output:/ [2, 3]

console.log([1,2,2,3].intersect2([2,3,4,5]));output:/ [2, 3]

console.log([1,2,3,3].intersect2([2,3,4,5]));output:/ [2, 3]

console.log([1,4,3,2].intersect2([2,3,4,5]).sort());output:/ [2, 3, 4]

console.log([1,2,3,3].intersect2({'first':1,'second':2}));output:/ [1, 2]

<script>Array.prototype.contains = function(e) { /* * Check the array has a given value * * @param (int or string|required) e - array * @return (array) If has give value return true, If not return false */ var type = typeof e; if( type != 'function' && type != 'object') { for (i in this) { type = typeof this[i]; if (this[i] == e && type != 'function' && type != 'object'){ return true; } } } return false; }; Array.prototype.intersect3 = function(e) { /* * Get intersection between two array * * @param (array|required) e - array * @return (array) Intersection between two array */ var tmp = []; for(i in this){ var val = this[i]; if(e.contains(val) && tmp.indexOf(val) == -1){ tmp.push(val); } } return tmp; }</script>

This sample same as previous one. There has a one thing to difference, Used two javascript prototype method to get intersection into two arrays. first method use to check a array, it will has a given value. Second method like as previous sample's methods. try to get intersection between two array.

These two methods are must be needed to get intersection. Code intersect3 method every time after contains method.

Example:

console.log([1,2,3].intersect2([2,3,4,5]));output:/ [2, 3]

console.log([1,2,3].intersect2([2,3,3,4,5]));output:/ [2, 3]

console.log([1,2,2,3].intersect2([2,3,4,5]));output:/ [2, 3]

console.log([1,2,3,3].intersect2([2,3,4,5]));output:/ [2, 3]

console.log([1,4,3,2].intersect2([2,3,4,5]).sort());output:/ [2, 3, 4]

intersect2 method can be used to get intersection between two arrays and also object type arrays. but other methods cannot be execute on object type arrays.
asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila