Simplest Code For Array Intersection in JavaScript

Simplest code for array intersection in javascript

Use a combination of Array.prototype.filter and Array.prototype.includes:

const filteredArray = array1.filter(value => array2.includes(value));

For older browsers, with Array.prototype.indexOf and without an arrow function:

var filteredArray = array1.filter(function(n) {
return array2.indexOf(n) !== -1;
});

NB! Both .includes and .indexOf internally compares elements in the array by using ===, so if the array contains objects it will only compare object references (not their content). If you want to specify your own comparison logic, use Array.prototype.some instead.

How to calculate intersection of multiple arrays in JavaScript? And what does [equals: function] mean?

I wrote a helper function for this:

function intersection() {
var result = [];
var lists;

if(arguments.length === 1) {
lists = arguments[0];
} else {
lists = arguments;
}

for(var i = 0; i < lists.length; i++) {
var currentList = lists[i];
for(var y = 0; y < currentList.length; y++) {
var currentValue = currentList[y];
if(result.indexOf(currentValue) === -1) {
var existsInAll = true;
for(var x = 0; x < lists.length; x++) {
if(lists[x].indexOf(currentValue) === -1) {
existsInAll = false;
break;
}
}
if(existsInAll) {
result.push(currentValue);
}
}
}
}
return result;
}

Use it like this:

intersection(array1, array2, array3, array4); //["Lorem"]

Or like this:

intersection([array1, array2, array3, array4]); //["Lorem"]

Full code here

UPDATE 1

A slightly smaller implementation here using filter

How can I check two arrays at the same index?

.map (either at the top level or the nested one) doesn't make sense, because you aren't trying to transform each element of one array into another. If you want to use an array method, use .reduce instead, and use the index in the callback to access the associated element in the other array to see if it's equal.

const selections = ["one", "two", "three"];
const allCorrectAnswers = ["four", "two", "three"];
const totalCorrect = selections.reduce(
(correctSoFar, answer, i) => correctSoFar + (answer === allCorrectAnswers[i]),
0
);
console.log(totalCorrect);
// setScore(totalCorrect);

Simplest code for array intersection in javascript

Use a combination of Array.prototype.filter and Array.prototype.includes:

const filteredArray = array1.filter(value => array2.includes(value));

For older browsers, with Array.prototype.indexOf and without an arrow function:

var filteredArray = array1.filter(function(n) {
return array2.indexOf(n) !== -1;
});

NB! Both .includes and .indexOf internally compares elements in the array by using ===, so if the array contains objects it will only compare object references (not their content). If you want to specify your own comparison logic, use Array.prototype.some instead.

Find common elements from arrays's which are inside an Object - Javascript

a possible approach using reduce and Set. for the accumulator I'm initializing it with the first array (x.c). Doesn't matter which one since in the end we are taking intersection anyway. Inside the reduce I'm intersecting the accumulator with the current iteration array

const x = {
c: ['Full_Name', 'Last_Name', 'Email', 'Organizations', 'MISC', 'UNIQUEID'],
small: ['Title', 'Organizations', 'URL', 'UNIQUEID'],
big: ['abc', 'Organizations', 'def', 'UNIQUEID']
}

const res = Object.values(x).reduce((acc, curr) => {
return new Set(curr.filter(i => acc.has(i)))
}, new Set(x.c))

console.log([...res])


Related Topics



Leave a reply



Submit