我想基于给定值返回对象中的键数组
Here's a simple way of doing it with Object.keys()
:
const keys = Object.keys(obj2).filter(k => obj2[k] == "02");
This will make keys
an array of keys in obj2
whose property is "02"
.
You get keys of object using Object.keys()
and then filter()
the keys which have value equal to given value.
const obj2 = {a: '01', b: '02', bb: '02', bbb: '02'}; function findKeys(obj,value){ return Object.keys(obj).filter(key => obj[key] === value); } console.log(findKeys(obj2,'02')); //["b","bb","bbb"] console.log(findKeys(obj2,'01')); //["a"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.