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.

Read Also:  how to allow only 10 digit mobile number validation using javascript with jquery

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]

 $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)
            }
        })
    })
}

Example : intersection using javascript

	
		<title>javascript intersection of two arrays</title>
		<img src="image/gif;" class="mce-object" width="20" height="20" alt="<script>" title="<script>" />
	


<h1> intersection of two arrays in JavaScript</h1>

	
	


javascript – Find all the intersecting elements amongst arrays

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

Read Also:  javascript Text Avatar Plugin Like Gmail

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).

// 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

// 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

// 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