Lodash _.some无法正常工作

时间:2016-12-12 20:22:48

标签: javascript lodash

我尝试使用lodash来查找对象数组中是否存在key,但即使它们在那里也似乎找不到它们:

var dtRng = ["1420070400000","1422748800000","1425168000000", etc];

var scnFtprntDates = { "values": [
    { "key": "1420070400000", "values": 289819.9054 },
    { "key": "1422748800000", "values": 289819.9054 },
    { "key": "1425168000000", "values": 289819.9054 },
    etc
]};

var scn2 = scnFtprntDates.values
var lngth = scn2.length

for (var q = 0; q < dtRng.length; q++) {
    if (_.some([scn2], ["key", dtRng[q]])) {
        console.log("found");
    } else {
        console.log("not found"); 
    }
}

似乎在控制台日志中返回所有not found ...

2 个答案:

答案 0 :(得分:3)

您正在将您的收藏包装在一个额外的数组中,删除方括号

if (_.some(scn2, ["key", dtRng[q]])) { 
  ....

答案 1 :(得分:0)

你不需要lodash,这个简单的单行为你做:

dtRng.forEach(k => { const result = scnFtprntDates.values.filter(obj => obj.key === k).length > 0 ? 'found' : 'not found'; console.log(k, result) })