【问题标题】:Iterate through an Array of Objects within another Array of Objects JavaScript [closed]在另一个对象数组 JavaScript 中遍历一个对象数组 [关闭]
【发布时间】: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


【解决方案1】:

您可以结合使用 forEach 方法和 reduce 方法来遍历 secondArray 中的对象,将所有 userRating 元素相加,然后将答案记录到控制台。这是一个例子:

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
                     },
                 ]
                     
    },
];
let secondArray = firstArray.find(obj => obj.secondArray)?.secondArray || []; // to find the secondArray in firstArray and handle it not being present

let totalRating = secondArray.reduce((total, obj) => {
  return total + obj.userRating;
}, 0);

console.log(totalRating);

此代码将首先使用 find 方法在 firstArray 中找到 secondArray。如果它不存在,它将默认为一个空数组。然后,它将使用 reduce 方法将 secondArray 中的所有 userRating 值相加。最后,它将总评分记录到控制台。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-22
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 2021-12-21
    • 2021-12-15
    • 2021-10-16
    相关资源
    最近更新 更多