如何使用下划线js从2个对象数组中获取不同的值

How to get different values from 2 object arrays using underscore js

如何使用下划线 js 从 2 个对象数组中获取不同的值。 我有 2 个这样的对象数组

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
}];

我的结果应该是这样的

[{
    "asset_id": 5,
    "asset_type": 2
}, {
    "asset_id": 9,
    "asset_type": 3
}, {
    "asset_id": 10,
    "asset_type": 3
}]

underscorejs 中是否有任何内置函数

您可以使用 _.reject and _.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>