0

I have a JSON response like this:

    {
      "AD": {
        "name": "Andorra",
        "native": "Andorra",
        "phone": "376",
        "continent": "EU",
        "capital": "Andorra la Vella",
        "currency": "EUR",
        "languages": [
          "ca"
        ]
      },
      "AE": {
    "name": "United Arab Emirates",
    "native": "دولة الإمارات العربية المتحدة",
    "phone": "971",
    "continent": "AS",
    "capital": "Abu Dhabi",
    "currency": "AED",
    "languages": [
      "ar"
    ]
  },

And I want to iterate over the countries capitals, only. But as for now I can only get the key of the contry and i'm not being able to reach the nested properties. I'm using this code in my subscription:

subscribe(data=>{
        let cityProps=Object.keys(data);
        let cityList=[]
        for(let prop in cityProps){
          cityList.push(cityProps[prop]);

        }
        this.cities=cityList;
        console.log(cityList)

I understand that this logic will only give me an array of "AD","AE" and so on. But how can I get the names?

2 Answers 2

1

You have to use it like this -

for(let prop in cityProps){
     cityList.push(cityProps[prop].name);
}

const a = {
      "AD": {
        "name": "Andorra",
        "native": "Andorra",
        "phone": "376",
        "continent": "EU",
        "capital": "Andorra la Vella",
        "currency": "EUR",
        "languages": [
          "ca"
        ]
      },
      "AE": {
    "name": "United Arab Emirates",
    "native": "دولة الإمارات العربية المتحدة",
    "phone": "971",
    "continent": "AS",
    "capital": "Abu Dhabi",
    "currency": "AED",
    "languages": [
      "ar"
    ]
  }}
  
 const b = [];
 for(let prop in a){
     b.push(a[prop].name);
}
 
 console.log(b);

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

3 Comments

Ok, it works, but this only because I can modify my data structure; but what if I get this JSON from a remote server? That was my real issue
then also it should work, no need to modify data structure, it works as per you posted code in the question.
the problem is that I was still using object.keys with your updated answer...
0
var data = {
      "AD": {
        "name": "Andorra",
        "native": "Andorra",
        "phone": "376",
        "continent": "EU",
        "capital": "Andorra la Vella",
        "currency": "EUR",
        "languages": [
          "ca"
        ]
      },
      "AE": {
    "name": "United Arab Emirates",
    "native": "دولة الإمارات العربية المتحدة",
    "phone": "971",
    "continent": "AS",
    "capital": "Abu Dhabi",
    "currency": "AED",
    "languages": [
      "ar"
    ]
  }};

//this will return array of capitals
Object.values(data).map(e => e.capital); 

2 Comments

it's returning all the keys and values inside each country and it's not an array, but it's also a handy solution, thanks!
@Mellville Can you specify a sample response you are looking for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.