LoDash 挑战,我有一组需要过滤的对象,用于一组值
LoDash challenges, I have an array of objects I need to filter, for an array of values
我有以下对象数组,我需要使用 _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"
}];
你能试试关注吗
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);
这是我所知道的使用 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>
希望对您有所帮助。
我有以下对象数组,我需要使用 _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"
}];
你能试试关注吗
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);
这是我所知道的使用 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>
希望对您有所帮助。