javascript calculate Intersection of multiple arrays

javascript calculate Intersection of multiple arrays

Today, We want to share with you javascript calculate Intersection of multiple arrays.
In this post we will show you javascript intersection multiple arrays example, hear for we will give you demo and example for implement.
In this post, we will learn about How to calculate intersection of multiple arrays in JavaScript with an example.

Intersection (array_student ∩ array_teacher):

create array_student set that contains all those elements of set array_student that are also in set array_teacher.
Suppose we have two array, array_student and array_teacher which all the contains person’s name and person’s age.
and now intersection to We will get the(both array) intersection of two arrays based on person’s name.

The basic idea is to create an empty list(array) at the start,
and then loop through the original list array.
For each item in the original list array,
see if a matching item is already in your new list array. and
If so, increment the nbrItem property. Otherwise, in create a new object.

BASIC : Intersection of two array of objects Using angular Js

[code]
array_student = [{name : ‘shah pratik’,age : ’21’,},{name : ‘shailesh’,age : ’22’,},{name : ‘dharmivk’,age : ’23’,}]
array_teacher = [{name : ‘shah kinjl’,age : ’30’,},{name : ‘vishal’,age : ’25’,},{name : ‘hiren’,age : ’27’,}]
[/code]

[html]

$scope.fetchIntersectionOfArray = function(array_student,array_teacher){
arrayfinal = []
angular.forEach(array_student, function(value,index){
angular.forEach(array_teacher, function(object,index1){
if(value.name==object.name){
arrayfinal.push(object)
}
})
})
}
[/html]

Example : intersection using javascript

[html]

javascript intersection of two arrays
<script>

intersection of two arrays in JavaScript

[/html]

javascript – Find all the intersecting elements amongst arrays

First off, a small note on usid in associative arrays in JavaSript :

JavaScript does not used really support associative arrays (array type)but since all arrays are objects(chunks),
associative arrays most of the can be emulated by using it as an object(chunks).

[html]
// javascript Actual array form
var natureList = new Array(0,1,2,3,4,5,6,7,8,9,10,87,98);
alert(natureList.length); // 13
[/html]

[html]
// JavaScript Actual Object emulated as well as Associative array(list)
var natureList = {‘zero’:0, ‘one’:1, ‘two’:2, ‘three’:3, ‘four’:4, ‘five’:5, ‘six’:6, ‘seven’:7, ‘eight’:8, ‘nine’:9}
alert(natureList.length); // undefined
var ctest = 0; for (var n in natureList) ctest++; alert(ctest); // 10
[/html]

[html]
// javascript Now add a ‘function’ to the all natureList add in ‘class’
natureList.add = function(a,b) { return a + b; }
ctest = 0; for (var n in natureList) ctest++; alert(ctest); // 11
[/html]

Leave a Comment