0

I am trying to loop through the nested object below and display the value of each iteration.

const data = {
         "0": {
                "identity": {
                      "personal": {
                             "first_name": "John",
                             "last_name": "Doe",
                       },
                      "identity": "j.doe@gmail.com",
              },
        },
        "1": {
                "identity": {
                      "personal": {
                             "first_name": "Andrea",
                             "last_name": "Watson",
                       },
                      "identity": "a.watson@outlook.com",
              },
        },

I have a background with python so es6 and javascript is not really my strength.

Thanks!

John D
  • 285
  • 3
  • 22
  • For starters: `Object.values(data).forEach(identity => console.log(identity.personal.last_name))` –  Sep 04 '20 at 12:37
  • @ChrisG thanks for the solution. what is the best way to assign the result to a variable instead of using `console.log` as i need to pass the result to a table built in the codebase? Thank you. – John D Sep 04 '20 at 12:49
  • Use [`.map()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) –  Sep 04 '20 at 12:53
  • @ChrisG `const solution = Object.values(results).map(identity => identity)` i am using this but it does not seem to work, using `map` instead of `forEach` – John D Sep 04 '20 at 12:55

0 Answers0