gpt4 book ai didi

javascript - 如何使用值获取子对象键?

转载 作者:行者123 更新时间:2023-11-30 11:31:54 25 4
gpt4 key购买 nike

我有如下嵌套对象,

 {
"Types": {
"TEST": {
"name": "EC",
"alloweMultipls": "true"
},
"TEST1": {
"name": "ED",
"alloweMultipls": "true"
}
}
}

如果我只有键 "EC" 而没有任何其他值,我该如何检索 allowMyltipls

最佳答案

试试这个方法。您可以获得 fileTypes 对象的所有属性,然后使用 .filter 函数遍历它们并找到 name 等于您的属性。然后使用该键检索嵌套对象。

const object = {
"fileTypes": {
"TEST": {
"name": "EC",
"alloweMultipls": "true"
},
"TEST1": {
"name": "ED",
"alloweMultipls": "true"
}
}
};

const key = 'EC';
const fileTypes = object.fileTypes;

const property= Object.keys(fileTypes).filter(item => fileTypes[item].name === key);

console.log(fileTypes[property]);

关于javascript - 如何使用值获取子对象键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45931277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com