我对复杂的 json 对象感到困惑,在下面的示例中,如何将具有 entreprise : 'microsoft'
的整个对象存储在变量中。
clientList = {
id-1111 : {
entreprise : 'facebook',
president : 'Mark',
},
id-2222 : {
entreprise : 'microsoft',
president : 'Bill',
},
id-3333 : {
entreprise : 'apple',
president : 'Tim'
}
}
例如,我动态获取“Microsoft”,并且我想获取作为输出:
{
entreprise : 'microsoft',
president : 'Bill'
}
我知道这是一个基本问题,但我为此苦苦挣扎了几个小时。 感谢您的帮助。
最佳答案
您可以使用find
从列表中查找单个项目。
var clientList = {
"id-1111": {
entreprise : 'facebook',
president : 'Mark',
},
"id-2222" : {
entreprise : 'microsoft',
president : 'Bill',
},
"id-3333" : {
entreprise : 'apple',
president : 'Tim'
}
};
var result = Object.values(clientList).find(x => x.entreprise == "microsoft");
console.log(result);
关于JavaScript 对象 : select the whole object that has known key : value pair,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52594336/