javascript get intersection of two arrays

Solutions on MaxInterview for javascript get intersection of two arrays by the best coders in the world

showing results for - "javascript get intersection of two arrays"
Alessandra
13 Oct 2019
1let intersection = arrA.filter(x => arrB.includes(x));
Leonie
07 Jan 2019
1function getArraysIntersection(a1,a2){
2    return  a1.filter(function(n) { return a2.indexOf(n) !== -1;});
3}
4var colors1 = ["red","blue","green"];
5var colors2 = ["red","yellow","blue"];
6var intersectingColors=getArraysIntersection(colors1, colors2); //["red", "blue"]
7
8
Francesca
18 Aug 2016
1// Generic helper function that can be used for the three operations:        
2const operation = (list1, list2, isUnion = false) =>
3    list1.filter(
4        (set => a => isUnion === set.has(a.userId))(new Set(list2.map(b => b.userId)))
5    );
6
7// Following functions are to be used:
8const inBoth = (list1, list2) => operation(list1, list2, true),
9      inFirstOnly = operation,
10      inSecondOnly = (list1, list2) => inFirstOnly(list2, list1);
11
12// Sample data
13const list1 = [
14    { userId: 1234, userName: 'XYZ'  }, 
15    { userId: 1235, userName: 'ABC'  }, 
16    { userId: 1236, userName: 'IJKL' },
17    { userId: 1237, userName: 'WXYZ' }, 
18    { userId: 1238, userName: 'LMNO' }
19];
20const list2 = [
21    { userId: 1235, userName: 'ABC'  },  
22    { userId: 1236, userName: 'IJKL' },
23    { userId: 1252, userName: 'AAAA' }
24];
25  
26console.log('inBoth:', inBoth(list1, list2)); 
27console.log('inFirstOnly:', inFirstOnly(list1, list2)); 
28console.log('inSecondOnly:', inSecondOnly(list1, list2));
queries leading to this page
find common elements form multiple arrays javascriptmatch two arrays if exists jsarray matchingjs get intersection of two arraysget only intersection of all arrays of objectsjavascript determine intersection between 2 arraysjs get intersect elementsjavascript common values of two arraysjs find intersection of arraysintersect 2 arrays javascriptlist take only common value jsdetect interseection jscheck if 2 arrays have a matching element javascriptjavascript check if item present in two arrayshow to get the common items from two array in javascriptrschedule javascript intersectionfind common between two arrays javascriptfind intersection between two arrays of objects javascriptjavascript array set differencejavascript intersecthow to find intersection between two arrays of strings javascriptintersection in javascript with multiple values as arrayhow to check if there is 2 same elments in an array javascriptfind intersection of two arrays in javascriptjs get common values from arraysjavascript common elements in many arrayshow i get common number from array in jsjs array return elements exists in both arrayscheck if multiple value exists in array javascriptfilter common elements from two arrays javascriptjavascript array common elementsreturn array from multiple arrays with common elements javascriptintersection of two array of objects javascriptget intersection within object of arraysjavascript intersection two arrayssee intersection of two arrays javascriptget common values from two arrays javascriptjavascript intersection of two object arrays javascript array intersection es6find intersection between at least one array jsget overlap between two arrays jscheck if two arrays contain same elements jsintersection of array in javascript with tolerance valuejavascrip check array exists multiple itemtaking common from two arrays javascriptjavascript intersection of multiple arrayscheck if two arrays share an elementfind intersection of two arrays of objects typescriptintersection javascript arrayintersection between two arrays jsjavascript array intersection multiple arrayshow to find common elements in two arrays in javascriptintersection of lists javascriptarray intersection angularhow to overlap arraysintersection of array not workget intersection of two arrays javascriptjavascript check if two arrays contain same elementsjs intersection functiones6 array intersectionintersect three arrays jsjavascript find intersection of elementshow to check two index have same elements in same array javascriptoverlap two arrays javascriptjs common array itemsfind intersection of two strings jsintersection of two arrays in javascriptjs array intersection return booleanfind the intersection of two arrays javascriptjs intersect two arraysjavascript intersection jsarray intersect jsfind non common elements in two arrays jsjs intersect two arraysarray overlapfilter a common element javascriptjs get items that exist in two arrayjavascript check for 2 values in arrayintersection of array of objectsarray intersect in jsintersection and union using javascriptintersection two arrays javascriptjs 2 arrays for each element which exists in bothfind intersection of two arrays of objects javascriptjs set find commonintersecting elements in arrays javascripthow to imntersect an arrayjs array overlapintersection between two arrays javascriptjs check if array contains two valuesjs get last intersected elementjava scriptintersectionts array intersectionjs intersect arrayesintersection of two arrays ii javascripthow to check intersection of two arrays in javascript intersection of two arrays of objects get common in two arrays jsjavascript array overlapintersection of arrays javascriptintersection of 2 arrays jsjavascript any intersection in arraysjavascript find common elements in two arraysarray intersection in jsfind common objects in two arrays javascriptcan you find intersection of array of objects in javascripttypescript intersection of two arraysarray intersectionfinding common elements in a single arrayfind intersection of two arrays elements javascriptintersection of two sorted arrays javascriptarray intersect in javascriptjavascript array intersection beetween two arraysjs intersection of two arraysjavascript get intersection of two arrays of objectsreturn intersection of two lists javascriptif two arrays have matching values javascriptjavascript find if any value between two arrays matchjs array intersect callbackjs check common in 2 arraysreturn an array of common elements jsjavascript get elements that exist in two arraysjavascript show intersection between two arrayshow to check if 2 arrays have same elements in javascriptfind common elements in 2 array of objects nodejs how to get to an element with two arrays in jsjavascript intersection of two arrays objectsfind the intersection of two values in an arrays javascriptintersection between arrays valuesget common values in array jshow to find all the common multiples in two arrays javascriptintersect array javascriptintersection of multiple arrays javascriptjavascript efficient array intersecthow to intersect arrays in javascript without filter 28 29javascript get intersection of two arraysjavascript object intersection with array of valuesintersection for array of objects in javascriptintersect two arrays javascriptjs intersection arraysmany arrays intersect jsintersection of two arrays containing objectsjs intersect 2 arraysintersection of two arrays javascriptdistinct elements in js return array overlap javascriptnodejs array intersectionjavascript intersection of two arrays iihow o get index the intersection of two arrays jsintersection of two arrays ii solution javascriptjs arrays intersectarray intersect javascriptjs find intersect countfinding mutually exclusive elements in 2 arrays javascriptfind intersection javascriptjs arrya intersctionjavascript string array intersectionphp get intersection of arraysintersection in javascript arraysarray intersect javascripthow to check if two elements in array equal a number javascriptjavascript getting a intersection of two listshow to perform intersection using javascriptjavascript intersect arrayscheck if items are equal in two arrays javascriptget multiple array common elements javascriptcompute intersection of two object arrays in javascriptjavascript exists in both arrayhow to check two arrays are permutations of each other java collectionsperform setdifference in json es6typescript find intersection between two setsfind the array matchingjavascript array comonhow to return array with two integers in javalist of values that are intersection of all the arrays in javascriptjs get common array elementshow to find common elements in two arrays javascriptintersections in arrays javscriptfind intersection between two object arrays javascriptintersection array in jsjs get intersect elements htmlfuntion for intersection in javascripthow to check if one array has 2 same items jsfind common value from array javascriptjs intersectjavascript outer intersect two arraysinteresection of two arrays javascriptaccessing an 40d array elements when coordinates are given in pair c 2b 2bfind a intersection of two arrays jsjs intersect two arrayfastest way to find intersection of two lists javascripthow to code a function that takes two arrays and returns a new array that is the intersection of both arrays jsintersection of array javascriptcheck if the array constists of two elements in jsfind common elements in multiple arrays javascriptjavascript two array common elementsis there a simple mthod that sums all elements in an arrayjs check items match two listjs check if array of array includes two values conditiontypescript union of 2 arrayintersection in javascript with multiple values as aarrayjavascript two arrays intersectionjs arrays intersectionintersection of 2 arrays javascripthow to make elment intersection in jsintersection javascriptjs check if two arrays contain same elementshow to check two value in array if exist or not javascriptintersection of three arrays javascriptintersect 2 similar arrays javascript into objectjavascript array intersectionintersection in es6react javascript array intersectionhow to check if array contains two element javascriptarray intersect javasciprlink words in different string arrays c 23check values of 2 arrays jsarray intersection javascriptintersection number of array in javascriptmatching arrayfind intersection of two arrrays javascripthow to intersect two arrays in javascriptstring array intersection javascriptjs two array intersectionintersection of two large arrays javascript without javascript predefined functioncompare two objects by path or properties lodasharray matching in javascriptintersection of two arrays typescriptjs get items not containing in 2 arraysintersection of objects in two arrays jshow to find value exist in array two dimonds javascriptjs array intersectarray intersect maintain rightfind intersection of two arrays javascriptget intersection of two array of objects javascriptget intersection values in two arrays typescriptintersection of two arrays ii typescripthow to check if a values is in 2 array jsjavascript see if two arrays intersectintersection of two arrays of objects javascripthow to get intersection of two arrays javascripta intersect b javascript arrayfind intersection in array javascriptphp get 2 arrays and compare themjavascript array union intersectiontypescript array interset does not existtwo array check if value exists javascripthow to check any with two array value in array javascripthow to access same index values in 2 different arrays pythoncheck if intersection of arrays javascriptcheck if value exists more than once in array javascriptget intersection elements from two or more arrays javascriptjavascript check intersection between two arraysintersection of two arrays observalbe arraysif two elements are included in an array javascriptmatching arrayshow to select a subsection of an object intersection javascriptjs array intersectiojavascript do arrays intersectarray filter skipping elemnet when finding intersection with includes in javascriptjavascript intersectionshow to check if 2 different arrays match javascriptintersection two arrays javascript objectget the intersection of two arrays jsintersection of two object arrays javascript by idstring array to intersection typescriptget intersection of two arrays jsjs array intersection objectjavascript check two elements in arrayinterection in jsintersection in javascripthow to get common result from 2 arrays in javascriptintersects method jsintersection of set and array javascriptarray intersection in javascriptfind same elements in two arrays javascriptget the intersection of 2 arrays jsget intersection of two array of lists javascriptjs array intersectionangular array intersectioncompute intersection of two object arrays in javascript 5bduplicate 5djavascipt get the number that exist in two listsnodejs get common elements in two arrayscheck if value exists in two arrays javascriptintersection values of two array in jsfind intersection between 2 arrays javascriptarray intersect keep rightintersection of arrays jsfind common value in array javascripthow to check if 2 elements are present in an array in javascriptcommon with two array jsintersection array objecty typescriptintersection of two arrays javascript solutionnodejs get find common value on multiple arrayintersection of two lists javascripthow to check array two index have same elements javascriptcheck items of two arrays in javascriptget mutual part of 2 array javascriptinstersect array jsphp get 2 arrays and compare them and get valuesjavascript find common elements in two arrayhow to find the intersection between two arrays jsjs check if arrays overlapintersection of array in javascriptarray of objects intersection javascriptfinding intersection of arrays in javascriptget list of intersecting elements 22html 22js array intersectjs if arrays overlapjavascript two arrays contain same elementscheck if an element appears in two array javascriptjavascript find intersection of two array of objectsintersection of two objects in javascriptjavascript array find common valuesin arrays phpjsfind intersection of two arrays javascript objectget only intersection of multiple arrays of objectsnode js how to check array contain value multiple valuescalcular intersection of two arrays javascriptjs 2 array intersectionintersection of two list in javascriptjs check if two arrays have same elementsjavascript get intersection of an array of objectsfind common elements in arrays javascripthow to get intersection value from an arrays javascriptjavscript array intersecttwo array intersection javascriptintersection of 2 arraysreturn common elements in array jsjavascript get array intersectionjavascript get intersect arrayarray intersection typescriptintersection between two array of objects javascriptjavascript two array intersectioncheck if an element in an 2 array is the same jsfind exist two arraycheck 1 array item exist in two array in javascriptjavascript intersection between two arraysintersect array in reactget common elements from two arrays javascriptget list of intersecting elements 22html 7c 22intersect arrays javascriptcheck if number in two array javascriptjs array exists in both arraysfind intersections in arrays javscriptjs find overlap in arrayjavascript intersection of two arraysjavascript two list common elementscode to take intersection of arrays js o 28n 29how to filter 2 arrays in javascript notintersectionhow to get common data from two different array in javascripthow to intersection in js in two arraysselect elements in both arrays javascriptjs check if multiple element is present in arrayjavascript intersection of 2 arraystwo arrays are equal if they contain the same elements javascriptif array intersection is not empty typescriptcheck intersection of two arrays javascriptintersection array of objects jsjavascript intersect two array fasttwo array intersect value get in jsjs intersection between 2 arrayshow to check if values in two arrays overlap in javascriptintersection of object array with new setintersection of two array of objects javascriptarray intersection objects typescriptintersection of two array in javascripthow to find intersection of 2 arrays of different types using javascriptintersection with filter jslist intersection in typescriptnew array not working when returns correct intersectioncheck if multiple elements are present in an array javascriptget common data from 2 array jshow to check if an item is included in 2 arrays javascriptjavascript check if two array same elements on same indexcheck items match in two arrays javascripthow to get multiple elements using stream 28 29 mapjavascript intersect arrayjavascript 2 arrays intersectionhow to find data not intersection jsjavascript check if there 27s any overlap between arraysjs overlap bettween two arraysarray intersect in vue jsjavascript return common items of two arrayshow to check if two elements in array are equal jsjavascript two arrays any overlapfind common elements arrays javascripthow to filter 2 arrays in javascript not intersectionwrite a java program to find the common elements between two arrays of integerscheck if multiple values exists in array javascript intersection of strings to array typescriptfind common elements in two arrays javascriptitems that appear in both lists javascriptjs common elements in two setarray overlap javascriptfind common value from arrays jsarray prototype filter intersection of two arraysjs intersection of arrayjavascript intersect two arrasyintersection array of objects javascriptjavascript intersect two arraysintersect array jsarray intersection jsfind common elements two arrays javascriptfind which item is not included in two array differenc javascriptget 2 element in array javascripthjavascript intersection of two arraysjavascript if two arrays intersectget same elements two arrays javascriptjavascript array intersectjs if number differences intersectjavascript intersection of two arrays countjs find intersection javascript find array intersectionlists find union and intersection between lists and display the result javascriptsetdifference in json es6js find common elements in two arraysintersection of n arrays jsintersection 2 dimensional array javascriptreturn common elements of two arrays javascriptfind intersections of two array in javascriptneed to intersection of two array in javascriptfind intersection between two arrays javascriptjavascript get elements in both arraysget intersection object from two objects javascriptcompare two arrays and return common values javascriptjavascript intersection of two arrays of objects by valuestypescript intersect arrayfind two array element common items jsjavascript check if two arrays share an elementwhat is array intersectionhow to intersect array in reactjavascript get common elements two arraysjavascript check if array is one of two valuesjs intersection of 2 arraysintersectiom of arrays jsintersect sorted array javascriptjavascript find intersection of two arraysintersection array javascriptintersection of arrays javascriptsintersect jsjavascript intersection of arraysintersection of javascript arraysjavascript check if arrays intersectcheck if two arrays contain same elements javascriptif any intersection js arraysintersection arrays javascriptnode js intersection of arraysfind intersection of 2 arrays javascriptif zrray intersection is not empty typescriptjavascript how to chck if n item in array one is inside n item in array twoexits between two arrays javascripttypescript arrays intersectionjavascript overlapping arrayshow to get intersection of two arrays in javascriptcheck two array intersection javascripthow to intersect arrays in javascriptintersect arrays jsintersection of arrays in javascripttypescript intersection of two listsjavascript check if match in two arraysnodejs find intersection of two arrays numberintersection with at least one array jsjavascript object intersectionjavascript intersection arrayhow to find intersect elements in jsjavascript intersection of two arrays of objectsintersects 28 29 jsjavascript to find intersection of two arraycalculate intersection of two arrays javascriptjs two array of object intersectiondoes several values exist in array javascriptjs intersection arrayget common values from array javascripthow o get the intersection of two arrays jshow to check arrays two index have same elements javascripthow to find intersection of two arrays in javascript with setjavascript check if two arrays have same elements retunr new valuearray intersect javascript more than 2javascript intersectionjavascript intersect array valuescheck same value in two array jsfor in js two listsget intersection of two lists javascriptjavascript check if two arrays have same elementsjavascript check if value exists in two arrayscross section of array javascriptitems exist in 2 array javascriptintersection of two arrays javascript es6typescript array intersectionfind intersection of two arrays es6intersect js listintersection of two object arrays javascriptintersect javascripthow to use interset on arrays in javascriptintersection of elements in two arrays jshow to check if multiple value exists in array javascriptfind common element bet 2 number array jsjs set find common elements in two arraysjavascript intersection of two arrays string insensitiveinsersect arrays javascriptintersect array in javascripthow to use intersect on arrays in javascriptjs intersect arraysjavascript check if two arrays intersectjavascript intersection of two array of objectsarray intersect in javascriptwho to check if item in two array javascriptcode to take intersection of arrays jsjavascript common elements in two arrayshow to get common result from 3 arrays in javascriptjs list intersectioncheck if lists have intersection javascripthow to make elements in an array represent each otherintersection of two arrays using includes javascriptintersection between 2 arrays javascriptcommon elements in two arrays javascriptnodejs check if values in 2 arraysjavascript intersection of two listscheck if two elements in array are equal jsjs intercect of arrayjavascript find common values in two arrayshow to find intersection of two arrays in javascripthow to check if two elements in array equal a number jsjs fastest way to get common numbers between multiple arrayarray intersection javascript o 28n 29intersection of two arrays jscheck for overlap between two arrays jsjavascript array count common items in two arraysfirst common object between two arrayscheck intersection of two arrays jsintersection of two arraysjs get array intersectionintersection jswhat is the order of finding an intersection using javascriptonly two element in array javascriptfining intersection in javascriptjavascript get intersection of two arrays