_.intersection
Returns an array that is the intersection of all the arrays. Each value in the result is present in each of the arrays.
- // Underscore/Lodash
- console.log(_.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]))
- // output: [1, 2]
- // Native
- var arrays = [[1, 2, 3], [101, 2, 1, 10], [2, 1]];
- console.log(array.reduce(function(a, b) {
- return a.filter(function(value) {
- return b.includes(value);
- });
- })));
- // output: [1, 2]
- // ES6
- let arrays = [[1, 2, 3], [101, 2, 1, 10], [2, 1]];
- console.log(arrays.reduce((a, b) => a.filter(c => b.includes(c))));
- // output: [1, 2]
Browser Support for Array.prototype.reduce()
✔ | ✔ | 3.0 ✔ | 9.0 ✔ | 10.5 ✔ | 4.0 ✔ |
当前内容版权归 you-dont-need 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 you-dont-need .