Iterate through an Array of Objects within another Array of Objects JavaScript [closed]

I have created an Array of Objects. Within this Array of Objects, there is another Array of Objects.

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
                     },
                 ]
                     
    },
];

I want to loop through all of the objects in the Array named 'secondArray'. The program should then add all of the 'userRating' elements together and log the answer to the console.

The code i have tried did not work correctly:

for (let i of firstArray){
    console.log(element);
    for (let j of firstArray.secondArray) {
        console.log(j.userRating);
    }
}


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations