如何使用underscore.js在列表元素中过滤具有匹配参数的对象?

问题描述 投票:0回答:3
nodedatasource = [
    {"id":1,'name':'a'},
    {"id":2,'name':'b'},
    {"id":3,'name':'c'},
    {"id":4,'name':'d'},
]
list_ids = [3,2] #

我想获得一个过滤的数据源对象列表,其中列表ID在list_ids中?我尝试过这种方法。

...。需要更正]

datanode =_.filter(nodedatasource, function(element,ids){
    return element.id in ids
})
    

nodedatasource = [{“ id”:1,'name':'a'},{“ id”:2,'name':'b'},{“ id”:3,'name':'c '},{“ id”:4,'name':'d'},] list_ids = [3,2]#我想获取一个具有id ...

javascript underscore.js
3个回答
0
投票

如果您只想使用纯JavaScript进行此操作


0
投票
const list_ids = [3, 2]
const datanode =_.filter(nodedatasource,function(element){
                return list_ids.includes( element.id )
        })
//datanode = [{"id":2,'name':'b'},
//            {"id":3,'name':'c'}]

0
投票

另一种完全在underscorejs中完成的方法:

© www.soinside.com 2019 - 2024. All rights reserved.