我需要使用_id
值数组按_id
过滤以下对象数组。
var objects = [{
_account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
_id: "055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2"
}, {
_account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
_id: "LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp"
}, {
_account: "mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz",
_id: "rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61"
}, {
_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj",
_id: "944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB"
}, {
_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj",
_id: "rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK"
}];
var id_values = [
"rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK",
"944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB",
"rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61"
];
我认为difference
,pull
或without
可能有用,但除了LoDash的文档之外,我无法区分它们。我还认为forEach
,forIn
或uniq
可能很有用。我能够使用reject
过滤掉其中一个对象:
_.reject(objects, {
_id: "rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK"
});
这仍然无法帮助我按数组过滤。结果应该是以下数组:
var result = [{
_account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
_id: "055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2"
}, {
_account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
_id: "LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp"
}];
答案 0 :(得分:1)
你能试试吗
var data = [{ _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: '055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2'},
{ _account: 'YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo',
_id: 'LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp'},
{ _account: 'mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz',
_id: 'rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61'},
{ _account: 'AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj',
_id: '944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB'},
{ _account: 'AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj',
_id: 'rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK' }];
var ids = ['rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK', '944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB', 'rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61'];
data = _.filter(data,function(obj){
return ids.indexOf(obj._id) === -1;
});
console.log(data);
答案 1 :(得分:1)
这是我知道如何使用LoDash执行此操作的最快方式:
var objects = [{
_account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
_id: "055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2"
}, {
_account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
_id: "LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp"
}, {
_account: "mjj9jp92z2fD1mLlpQYZI1gAd4q4LwTKmBNLz",
_id: "rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61"
}, {
_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj",
_id: "944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB"
}, {
_account: "AaaraZrLqLfzRYoAPlb6ujPELWVW4dTK4eJWj",
_id: "rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK"
}];
var id_values = [
"rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK",
"944r40rPgPU2nXqzMYolS5nyo6Eo9OuqrlDkB",
"rEEwENwnznCQvkm61wRziKlMRPqaYztnR4vn61"
];
var index = index_of_array(id_values);
var result = _.reject(objects, function (object) {
return index[object._id] || false;
});
console.log(result);

<script src="http://cdn.jsdelivr.net/lodash/2.1.0/lodash.compat.js"></script>
<script>
/*
Creates an index of an array of keys. Allows you to check whether the array
has a specific key in O(1) time (as compared to O(n) time using indexOf). It
takes O(n) time to create the index where n is the length of the array.
e.g.
var keys = [1,2,3];
var index = index_of_array(keys); // O(n) time
console.log(index[1] || false); // true - O(1) time
console.log(index[4] || false); // false - O(1) time
console.log(keys.indexOf(1) >= 0); // true - O(n) time
console.log(keys.indexOf(4) >= 0); // false - O(n) time
*/
function index_of_array(array) {
var length = array.length
, result = {}
, index = 0;
while (index < length)
result[array[index++]] = true;
return result;
}
</script>
&#13;
希望有所帮助。