admin管理员组文章数量:1313731
I need something like lodash.intersectionWith but I also need duplicated values in result array.
Example:
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.intersectionWith(objects, others, _.isEqual);
Expected Result:
[{ 'x': 1, 'y': 2 },{ 'x': 1, 'y': 2 }]
Thanks in advance!
I need something like lodash.intersectionWith but I also need duplicated values in result array.
Example:
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.intersectionWith(objects, others, _.isEqual);
Expected Result:
[{ 'x': 1, 'y': 2 },{ 'x': 1, 'y': 2 }]
Thanks in advance!
Share Improve this question edited May 25, 2016 at 10:51 Emna Ayadi 2,4608 gold badges39 silver badges79 bronze badges asked May 25, 2016 at 10:16 MikhailMikhail 1,0282 gold badges14 silver badges30 bronze badges 2- It's not exactly clear why you expect there to be 2 copies of that object and not 3. – Omri Aharon Commented May 25, 2016 at 10:22
- Because array #1 has 2 values matching array #2 I'd guess – Randy Commented May 25, 2016 at 10:23
3 Answers
Reset to default 3You can find the intersection by filtering out items from the first array that don't match items in the second. Any duplicates in the first array will be kept.
var intersectwith = function(f,xs,ys){
return xs.filter(function(x){
return ys.some(function(y){
return f(x,y);
});
});
};
var equals = function(x,y){
return x === y;
};
console.log(intersectwith(equals, [1,2,3], [1,1,2,2,4]));
console.log(intersectwith(equals, [1,1,2,2,4], [1,2,3]));
Or, more readably, using ES6:
const intersectwith = (f,xs,ys) => xs.filter(x => ys.some(y => f(x,y)));
const equals = (x,y) => x === y;
console.log(intersectwith(equals, [1,2,3], [1,1,2,2,4]));
console.log(intersectwith(equals, [1,1,2,2,4], [1,2,3]));
Substitute _.isEqual
for equals
for paring objects: jsfiddle.
Useful documentation:
Array.prototype.filter
Array.prototype.some
You can make use of differenceWith() to get the difference between the source object
and the symmetric difference of the source object
and the others
object using xorWith().
var result = _.differenceWith(
objects,
_.xorWith(objects, others, _.isEqual),
_.isEqual
);
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
var intersection = _.intersectionWith(objects, others, _.isEqual);
var result = _.differenceWith(
objects,
_.xorWith(objects, others, _.isEqual),
_.isEqual
);
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.13.1/lodash.js"></script>
Use reduce
to validate each object
in the first array
, then check if that object
exists in the second array
. If it exists, reduce
will push
that object
into its array
.
The reduce
function will return that new array
automagically.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
var res = objects.reduce(
function(arr, obj){
if(containsObject(obj, others))
arr.push(obj);
return arr;
},
[]
);
function containsObject(obj, list) {
var x;
var ret = false;
list.forEach(function(s){
ret = JSON.stringify(s) == JSON.stringify(obj);
});
return ret;
}
console.log(res);
本文标签: lodashHow can i get arrays intersection include duplicate values using JavascriptStack Overflow
版权声明:本文标题:lodash - How can i get arrays intersection include duplicate values using Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741958325a2407118.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论