2015-02-06 43 views
1

我有以下數組的對象,我需要通過_id使用_id值的數組進行過濾。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" 
]; 

我相信differencepullwithout可能是有用的,但我不能從LoDash文檔告訴他們分開。我也認爲forEach,forInuniq可能是有用的。我能過濾的對象之一了使用reject

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

不過這並不能幫助我需要通過一個數組來過濾。結果應該是以下數組:

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

回答

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>

希望有幫助。

+0

感謝您的幫助。 – rashadb 2015-02-06 04:11:32

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

+0

我正在查看它! – rashadb 2015-02-06 03:33:09

+0

你的回答很完美!謝謝! – rashadb 2015-02-06 03:37:00

+0

使用'indexOf'非常慢,因爲它需要'O(n)'時間測試一個數組是否有給定的值。因此,如果data的長度是m,那麼你的代碼的時間複雜度就是'O(m * n)',這真的很糟糕。如果你創建了'ids'的索引,那麼你可以測試這個數組在'O(1)'時間內是否有給定的值。因此,「O(m * n)」時間減少到「O(m * 1)」時間。另外,它需要'O(n)'時間來創建索引。因此,總的時間複雜度是'O(m + n)',它明顯快於'O(m * n)'。查看我的答案瞭解更多詳情。 – 2015-02-06 04:11:12

相關問題