How to get different values from 2 object arrays using underscore js. I have 2 object arrays like this
如何使用下划线js从两个对象数组中获取不同的值。我有两个这样的对象数组。
var a = [{
"asset_id": 1,
"asset_type": 2
}, {
"asset_id": 1,
"asset_type": 3
}, {
"asset_id": 3,
"asset_type": 3
}, {
"asset_id": 5,
"asset_type": 2
}, {
"asset_id": 9,
"asset_type": 3
}, {
"asset_id": 10,
"asset_type": 3
}];
var b = [{
"asset_id": 1,
"asset_type": 2
}, {
"asset_id": 1,
"asset_type": 3
}, {
"asset_id": 3,
"asset_type": 3
}];
My result should be like this
我的结果应该是这样的。
[{
"asset_id": 5,
"asset_type": 2
}, {
"asset_id": 9,
"asset_type": 3
}, {
"asset_id": 10,
"asset_type": 3
}]
Is there any inbuilt functions to this in underscorejs
有什么内在的功能在下面的scorejs中?
1 个解决方案
#1
1
You could achieve it using _.reject
and _.findWhere
.
你可以用_来实现它。拒绝和_.findWhere。
var a = [{
"asset_id": 1,
"asset_type": 2
}, {
"asset_id": 1,
"asset_type": 3
}, {
"asset_id": 3,
"asset_type": 3
}, {
"asset_id": 5,
"asset_type": 2
}, {
"asset_id": 9,
"asset_type": 3
}, {
"asset_id": 10,
"asset_type": 3
}];
var b = [{
"asset_id": 1,
"asset_type": 2
}, {
"asset_id": 1,
"asset_type": 3
}, {
"asset_id": 3,
"asset_type": 3
}];
var newArr = _.reject(a, function(obj) {
return _.findWhere(b, obj)
})
console.log(newArr)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
#1
1
You could achieve it using _.reject
and _.findWhere
.
你可以用_来实现它。拒绝和_.findWhere。
var a = [{
"asset_id": 1,
"asset_type": 2
}, {
"asset_id": 1,
"asset_type": 3
}, {
"asset_id": 3,
"asset_type": 3
}, {
"asset_id": 5,
"asset_type": 2
}, {
"asset_id": 9,
"asset_type": 3
}, {
"asset_id": 10,
"asset_type": 3
}];
var b = [{
"asset_id": 1,
"asset_type": 2
}, {
"asset_id": 1,
"asset_type": 3
}, {
"asset_id": 3,
"asset_type": 3
}];
var newArr = _.reject(a, function(obj) {
return _.findWhere(b, obj)
})
console.log(newArr)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>