假設我有一個巨大的數組(100K記錄)集合可以說(sourceArray
)。我有一個另一個數組的列表(matchIds
)我需要使用從這個sourceArray
過濾。我如何用lodash實現這一目標?或純JavaScript?如何使用集合作爲謂詞來過濾數組
var sourceArray = [{
"id": 123,
},
{
"id": 456
},
{
"id": 789
},
{
"id": 111
},
{
"id": 222
},
{
"id": 333
}];
var matchIds = [123, 222];
下面的過濾器只針對單一項目
console.log(
_.filter(sourceArray, function(arr){
return arr.id === 123;
})
);
http://jsbin.com/fegipuwuwa/1/edit?html,js,console
什麼我真的希望是arr.id <..in..> matchIds
matchIds將是最大。 20個ID ..只有sourceArray是巨大的(有時100K到150K) – FakirTrappedInCode
@FakirTrappedInCode所以...這將使您從製作超過20個項目的150K索引中總結出3M浪費的操作。 – Redu
謝謝。假設matchIds = [123,222,999,1111],我只返回999,1111。正如我所看到的Iam實際上只對這些不匹配的ID感興趣,因爲我會再調用另一個API來返回這些ID的響應.. – FakirTrappedInCode