3

Expected effect: iterate objects in array, get value example 'email' in objects in array comments. I would like to spell the array and nested objects and arrays and return the 'email' value. When I get array comments I try to get value email in object I have error email is undefined

let scores =  [
  {
    "userId": 1,
    "id": 1,
    "title": "bbbb",
    "project": "JS",
    "completed": false,
    "comments": [
      {
        "itemId": 1,
        "id": 1,
        "name": "provident id voluptas",
        "email": "[email protected]",
        "body": "sdsdsd"
      },
      {
        "itemId": 1,
        "id": 2,
        "name": "provident id voluptas",
        "email": "[email protected]",
        "body": "sdsdsd"
      }
    ]
  },
 {
    "userId": 1,
    "id": 2,
    "title": "ggggg",
    "comments": [
      {
        "itemId": 2,
        "id": 1,
        "name": "odio adipisci rerum aut animi",
        "email": "[email protected]",
        "body": "dsdsdsd"
      }
    ]
  }
]


let obj;

for (var key in scores) {
      obj = scores[key];
      console.log(obj.comments); //return objects array
    }

 for (var key in ob) {
      let ob1 = ob[key];
      console.log(ob1[email]); //return email is undefined
    }
0

2 Answers 2

1

You need to iterate comments as well and get the property email.

With for ... of statement and destructuring assignment.

let scores = [{ userId: 1, id: 1, title: "bbbb", project: "JS", completed: false, comments: [{ itemId: 1, id: 1, name: "provident id voluptas", email: "[email protected]", body: "sdsdsd" }, { itemId: 1, id: 2, name: "provident id voluptas", email: "[email protected]", body: "sdsdsd" }] }, { userId: 1, id: 2, title: "ggggg", comments: [{ itemId: 2, id: 1, name: "odio adipisci rerum aut animi", email: "[email protected]", body: "dsdsdsd" }] }];

for (let { comments } of scores) {
    for (let { email } of comments) {
        console.log(email);
    }
}

With Array#forEach

let scores = [{ userId: 1, id: 1, title: "bbbb", project: "JS", completed: false, comments: [{ itemId: 1, id: 1, name: "provident id voluptas", email: "[email protected]", body: "sdsdsd" }, { itemId: 1, id: 2, name: "provident id voluptas", email: "[email protected]", body: "sdsdsd" }] }, { userId: 1, id: 2, title: "ggggg", comments: [{ itemId: 2, id: 1, name: "odio adipisci rerum aut animi", email: "[email protected]", body: "dsdsdsd" }] }];

scores.forEach(({ comments }) => comments.forEach(({ email }) => console.log(email)));

Sign up to request clarification or add additional context in comments.

4 Comments

How can I pull some objects without array . I have result comments:[{},{},{}]. I want result {},{},{}
you could take the object. what do you want to do with it?
pull objects from array comments and push to another array
just pusgh the object to a result array. for (let { comments } of scores) result.push(...comments);.
0

You would need a nested loop for all emails, but if you know the index of the email you are looking for then just pass that index to the comments[index]

scores[current_iteration].comments[current_iteration].email

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.