gpt4 book ai didi

javascript - 获取对象数组中的所有对象

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

我也是 reactjs 和 javascript 的新手。

在这里,我有一个对象数组,就像 ,

[
{
"id": "CSS",
"questionCount": [
{
"level": "TOUGH",
"type": "NON_CODE",
"count": "1"
}
]
},
{
"id": "Backbone",
"questionCount": [
{
"level": "TOUGH",
"type": "CODE",
"count": "2"
},
{
"level": "TOUGH",
"type": "NON_CODE",
"count": "5"
},
{
"level": "MEDIUM",
"type": "NON_CODE",
"count": "7"
},
{
"level": "EASY",
"type": "NON_CODE",
"count": "6"
}
]
},
]

现在,我想要的是一个对象数组,其中包含 questionCount 数组中存在的所有对象。所以,它会像,

[  {
"level": "TOUGH",
"type": "NON_CODE",
"count": "1"
}, {
"level": "TOUGH",
"type": "CODE",
"count": "2"
},
{
"level": "TOUGH",
"type": "NON_CODE",
"count": "5"
},
{
"level": "MEDIUM",
"type": "NON_CODE",
"count": "7"
},
{
"level": "EASY",
"type": "NON_CODE",
"count": "6"
} ]

那么,有人可以帮我解决这个问题吗?

最佳答案

您可以使用 Array.prototype.reduce()Array.prototype.concat() :

const temp = [{"id": "CSS","questionCount": [{"level": "TOUGH","type": "NON_CODE","count": "1"}]},{"id": "Backbone","questionCount": [{"level": "TOUGH","type": "CODE","count": "2"},{"level": "TOUGH","type": "NON_CODE","count": "5"},{"level": "MEDIUM","type": "NON_CODE","count": "7"},{"level": "EASY","type": "NON_CODE","count": "6"}]},];
const result = temp.reduce((a, c) => a.concat(c.questionCount), []);

console.log(result);

关于javascript - 获取对象数组中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53148651/

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