获取对象的键名,其值等于[重复项]

问题描述 投票:0回答:1

是否有更好的方法来获取值等于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' ]
javascript
1个回答
0
投票

ES6 +一线

let key = Object.keys(obj).find(k=> obj[k] === value);
© www.soinside.com 2019 - 2024. All rights reserved.