LoDash挑战,我有一个我需要过滤的对象数组,用于一组值

时间:2015-02-06 03:12:18

标签: javascript arrays functional-programming lodash

我需要使用_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"
];

我认为differencepullwithout可能有用,但除了LoDash的文档之外,我无法区分它们。我还认为forEachforInuniq可能很有用。我能够使用reject过滤掉其中一个对象:

_.reject(objects, {
    _id: "rEEwENwnznCQvkm61wZ9uey62Pjy5YTqgYGDK"
});

这仍然无法帮助我按数组过滤。结果应该是以下数组:

var result = [{
    _account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
    _id:      "055z5gzVyVfzlBnEOqYvcoLL1ZgOWJhkrWMkv2"
}, {
    _account: "YzzrzBrO9OSzo6BXwAvVuL5dmMKMqkhOoEqeo",
    _id:      "LvvrvyrOGOS2e5yE0Bdki45Y1ndVlgfoZ2zoOp"
}];

2 个答案:

答案 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);

Demo

答案 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;
&#13;
&#13;

希望有所帮助。