const arr1 = [{ id: 1 }, { id: 2 }]
const arr2 = [{ id: 1 }, { id: 3 }]
const intersection = arr1.filter(item1 => arr2.some(item2 => item1.id === item2.id))
// intersection => [{ id: 1 }]
const intersection = (a, b) => {
b = new Set(b); // recycling variable
return [...new Set(a)].filter(e => b.has(e));
};
console.log(intersection([1, 2, 3, 1, 1], [1, 2, 4])); // Array [ 1, 2 ]
Code Example |
---|
Javascript :: convert boolean to string javascript |
Javascript :: normal function vs arrow function |
Javascript :: delay sleep |
:: |
:: nodejs curd insert update delete |
Javascript :: download pdf from drive js |
Javascript :: present value formula js |
:: webpack test js or jsx |
Javascript :: |
:: beanstalk nodejs default port |
Javascript :: javascript multiple cases |
:: export socket io connection in react |
:: Get Input arrays |
Javascript :: jquery selector input name regex |
Javascript :: how to check if element is in viewport javascript |
:: |
:: $_GET data using javascript |
Javascript :: https request node.js output incomplete |
Javascript :: change the origin of html canvas |
:: does kendo window content clear on close |
Javascript :: getters and setters javascript |
:: |
Javascript :: VS Code Auto Import is bugging usestate |
:: string filter javascript |
:: disable eslint curly option |
:: find 401 error and logout axios in react |
:: |
:: adding hbs partials in express.js |
Javascript :: |
Javascript :: |