【发布时间】:2023-02-20 23:59:42
【问题描述】:
我创建了一个对象数组。在这个对象数组中,还有另一个对象数组。
let firstArray = [
{element: "This is a string"},
{element: "This is a string"},
{element: "This is a string"},
{element: "This is a string"},
{
element: "This is a string",
secondArray: [
{
otherElements: "This is a different element"
userRating: 5
},
{
otherElements: "This is a different element"
userRating: 5
},
{
otherElements: "This is a different element"
userRating: 5
},
{
otherElements: "This is a different element"
userRating: 5
},
{
otherElements: "This is a different element"
userRating: 5
},
]
},
];
我想遍历名为“secondArray”的数组中的所有对象。然后程序应将所有“userRating”元素加在一起并将答案记录到控制台。
我试过的代码不能正常工作:
for (let i of firstArray){
console.log(element);
for (let j of firstArray.secondArray) {
console.log(j.userRating);
}
}
【问题讨论】:
-
你有没有尝试过?如果是这样,你能和我们分享一下吗?
-
“我想要”不是问句。请向我们展示您尝试过的方法,并解释具体的问题所在。
标签: javascript arrays object iteration