JavaScript 对象 : select the whole object that has known key : value pair

标签 javascript json

我对复杂的 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/

相关文章:

javascript - 如何在 WebAudioApi 中配置 MediaElementAudioSourceNode?

json - 检查值是否为 typeof struct

python - json 存储未正确更新值 kivy

javascript - 当对象键存在于javascript中另一个对象的数组中时重构对象数据

javascript - 如何为多个用户使用人员选择器? SharePoint 2013

javascript - 使用 jQuery 检测按键何时被按下然后释放

javascript - 在 javascript 中将多值 Map<String, Object [ ]> 转换为 json

javascript - IFRAME 与 iPad safari 有很多问题

json - 使用自定义 MarshalJSON 更改结构中的 JSON 标签

javascript - 区分大小写的 URL 触发 CORS 错误