我可以通过以下方式筛选出纯净的列表:
但是我对过滤器还有进一步的要求,如下所示:
我的意思是,
var list = [1, 2, 3, 4, 5, 1, 3]
function list_filter(list, value) {
return list.filter(function (item) {
if (item === value) {
return true
}
})
}
console.log(list_filter(list, 1)) // there will print `[1, 1]`
但是我对过滤器还有进一步的要求,如下所示:
var a = [
{a:'1', b:"2", c:3, d: {id:'d1'}},
{a:'1', b:"2", c:3, d: {id:'d2'}},
{a:'1', b:"2", c:3, d: {id:'d1'}},
{a:'1', b:"2", c:3, d: {id:'d3'}},
{a:'1', b:"2", c:3, d: {id:'d3'}},
{a:'1', b:"2", c:3, d: {id:'d4'}}
]
我希望按照item.d.id
对列表进行筛选。
我想要这样使用list_filter
:
function list_filter(list, 'd.id', value) ... // the value is the filter id
或者:
function list_filter(list, ['d', 'id'], value) ...
我的意思是,
list_filter(a, ['d', 'id'], 'd1')
获取筛选后的列表。 ['d', 'id']
可能会变成 ['d', 'e', 'id']
或者更长。