【发布时间】:2022-01-18 00:02:52
【问题描述】:
如何按类型过滤项目?
const items = {
'someHash0': {
type: 'foo',
name: 'a'
},
'someHash1': {
type: 'foo',
name: 'b'
},
'someHash2': {
type: 'foo',
name: 'c'
},
'someHash3': {
type: 'baz',
name: 'd'
},
};
我想按 type=foo 过滤并得到结果:
const items = {
'someHash0': {
type: 'foo',
name: 'a'
},
'someHash1': {
type: 'foo',
name: 'b'
},
'someHash2': {
type: 'foo',
name: 'c'
}
};
我试过了
return _.mapValues(pairs, function (item) {
return (item.type === 'foo') ?? item
})
但它返回真/假而不是整个对象
【问题讨论】:
-
这是为了简化示例,在我实际使用的现金中,它是哈希值。 ps:为清楚起见进行了编辑
标签: javascript lodash