-1

I have a data like this:

{
    "countries": {
        "US": {
            "details": {
                "code": 1,
                "population": ""
            },
            "people": [
                {
                    "name": ""
                },
                {
                    "name": ""
                }
            ]
        },
        "UK": {
            "details": {
                "code": 44,
                "population": ""
            },
            "people": [
                {
                    "name": ""
                },
                {
                    "name": ""
                }
            ]
        }
    }
}

I want to loop through it and extract people so I can loop through the people and display them on the site.

var countries = {
    "countries": {
        "US": {
            "details": {
                "code": 1,
                "population": ""
            },
            "people": [
                {
                    "name": ""
                },
                {
                    "name": ""
                }
            ]
        },
        "UK": {
            "details": {
                "code": 44,
                "population": ""
            },
            "people": [
                {
                    "name": ""
                },
                {
                    "name": ""
                }
            ]
        }
    }
}


for (let [key, value] of Object.entries(countries.countries)) {
    //console.log(value);
        for (var people in value.people) {
                    console.log(people);
        }
}

But I keep getting 0, 1, how can I get the value of people array?

Rain Man
  • 1,163
  • 2
  • 16
  • 49
  • You should read about for..of and for..in I'll let the references here, [for..of](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/for...of) and [for..in](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Statements/for...in) – drocha87 Feb 17 '22 at 16:04

2 Answers2

1

For array (and other data structures implementing the iterable interface) it's for...of, not for...in (which is for iterating over keys of an object).

var countries = {
  "countries": {
    "US": {
      "details": {
        "code": 1,
        "population": ""
      },
      "people": [{
          "name": ""
        },
        {
          "name": ""
        }
      ]
    },
    "UK": {
      "details": {
        "code": 44,
        "population": ""
      },
      "people": [{
          "name": ""
        },
        {
          "name": ""
        }
      ]
    }
  }
}


for (let [key, value] of Object.entries(countries.countries)) {
  for (let people of value.people) {
    console.log(people);
  }
}
connexo
  • 53,704
  • 14
  • 91
  • 128
0

const obj =  {
    "countries": {
        "US": {
            "details": {
                "code": 1,
                "population": ""
            },
            "people": [
                {
                    "name": ""
                },
                {
                    "name": ""
                }
            ]
        },
        "UK": {
            "details": {
                "code": 44,
                "population": ""
            },
            "people": [
                {
                    "name": ""
                },
                {
                    "name": ""
                }
            ]
        }
    }
}
for (let key in obj.countries) {
    console.log(obj.countries[key].people);
}
Parvesh Kumar
  • 1,104
  • 7
  • 16