gpt4 book ai didi

javascript - 如何使用 Angular 根据父级和子级获取 key

转载 作者:行者123 更新时间:2023-12-01 01:26:38 24 4
gpt4 key购买 nike

这里我有一个像这样的对象数组

[{
"id": "company",
"checked": true,
"model": true,
"maker": true,
"country": true,
"enterprise": true
},
{
"id": "config",
"checked": true,
"ram": true,
"processor": true,
"hdd": true
}
]

当我尝试在这里获取对象键时,预期的结果是:

预期输出

["company", "checked", "model", "maker", "country", "enterprise"]

我得到的是:

["id", "checked", "model", "maker", "country", "enterprise"]

那么如何获取 id 和其余键的值?

最佳答案

尝试一下:

var yourArray = [{
"id": "company",
"checked": true,
"model": true,
"maker": true,
"country": true,
"enterprise": true
},
{
"id": "config",
"checked": true,
"ram": true,
"processor": true,
"hdd": true
}
];

var expectedResponse = yourArray.map(obj => {
var yourExpectedArray = Object.keys(obj);
var indexOfId = yourExpectedArray.indexOf('id');
yourExpectedArray[indexOfId] = obj['id'];
return yourExpectedArray;
});

console.log(expectedResponse);

关于javascript - 如何使用 Angular 根据父级和子级获取 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53711444/

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