2 回答
TA贡献1780条经验 获得超3个赞
您可以尝试使用Object.entries()
和.find()
作为:
const data = { 'aaa': { name: 'Profile one', created: 1587396315752, last_save: 1599936955429, }, 'bbb': { name: 'Profile two', created: 1594950234265, last_save: 1596064543536, }, 'ccc': { name: 'Profile three', created: 1586093327836, last_save: 1600022533585, } };
const search = 'Profile two';
const result = Object.entries(data)
.find(([k,v]) => v.name === search)[0];
console.log(result);
从文档中看到:
该
Object.entries()
方法返回给定对象自己的可枚举字符串键控属性 [key, value] 对的数组,其顺序与 for...in 循环提供的顺序相同
该
find()
方法返回所提供数组中满足所提供测试函数的第一个元素的值。
TA贡献1780条经验 获得超1个赞
您可以使用Object.entries()and找到它find():
let profiles = {
'aaa': {
name: 'Profile one',
created: 1587396315752,
last_save: 1599936955429,
},
'bbb': {
name: 'Profile two',
created: 1594950234265,
last_save: 1596064543536,
},
'ccc': {
name: 'Profile three',
created: 1586093327836,
last_save: 1600022533585,
}
};
let id = Object.entries(profiles).find(e => e[1].name === 'Profile two')[0];
console.log(id);
添加回答
举报