【发布时间】:2020-06-03 17:07:38
【问题描述】:
有没有更好的方法来获取值等于cat 的对象的键名?
import { toPairs } from 'lodash'
const options = {
x: 'cont',
y: undefined,
color: 'cat',
radius: 'cat'
}
const catNames = toPairs(options).map(([name, type]) => type === 'cat' ? name : null).filter(n => n)
// [ 'color', 'radius' ]
【问题讨论】:
-
Object.keys(options).filter(k => options[k] === 'cat') -
@Ali Right,这就是我们所要求的。