6

I'm trying to write a method which will help me return an array of the object keys of all the currencies. But, I'm stuck at a point where I get the complete array of objects with key, value pair.

And yes, I primarily need to use ES6 methods. I wouldn't want to use any other iterators.

For e.g. : What I need:

['AED', 'ALL', 'AUD', 'EUR' .....]

What I get :

[{AED: {"isDefault": true}}, {ALL: {"isDefault": true}}, {AUD: {"isDefault": true}}, {'EUR': {"isDefault": true}}.....]

Could you please help me achieve this?

Here's the code:

var myJSON = {
   "countryCode": {
   "Australia": "AU",
   "United States": "US",
   "Britain": "GB",
   "Japan": "JP",
   "India": "IND",
   "France": "FR",
   "Russia": "RS"
 },
 "countries": {
  "AE": {
    "currencies": {
   "AED": {
     "isDefault": true
   }
    }
  },
  "AL": {
    "currencies": {
   "ALL": {
     "isDefault": true
   }
    }
  },
  "AU": {
    "currencies": {
   "AUD": {
     "isDefault": true
   }
    }
  },
  "US": {
    "currencies": {
   "USD": {
     "isDefault": true
   }
    }
  },
  "GB": {
    "currencies": {
   "EUR": {
     "isDefault": true
   }
    }
  },
  "FR": {
    "currencies": {
   "EUR": {
     "isDefault": true
   }
    }
  },
  "JP": {
    "currencies": {
   "JPY": {
     "isDefault": true
   }
    }
  },
  "RS": {
    "currencies": {
   "RSD": {
     "isDefault": false
   }
    }
  },
  "ZA": {
    "currencies": {
   "ZAR": {
     "isDefault": true
   }
    }
  }
   }
 };
 
 function getData() {
   const myArr = Object.keys(myJSON.countries).map((k) => myJSON.countries[k]);
   console.log(myArr); 
   const myArr1 = myArr.map((currency) => currency.currencies);
   console.log(myArr1);
   const myArr2 = myArr1.map((key, value) => key);
   console.log(myArr2);
 }
<button onclick="getData()">Get Data</button>
Sunny
  • 902
  • 3
  • 18
  • 41

4 Answers4

7

You could take the first key of the objects.

myArr1.map((key, value) => Object.keys(key)[0]);

function getData() {
    const result = Object
            .keys(myJSON.countries)
            .map(k => myJSON.countries[k])
            .map(({ currencies }) => currencies)
            .map(currency => Object.keys(currency)[0]);

    console.log(result);
}

var myJSON = { countryCode: { Australia: "AU", "United States": "US", Britain: "GB", Japan: "JP", India: "IND", France: "FR", Russia: "RS" }, countries: { AE: { currencies: { AED: { isDefault: true } } }, AL: { currencies: { ALL: { isDefault: true } } }, AU: { currencies: { AUD: { isDefault: true } } }, US: { currencies: { USD: { isDefault: true } } }, GB: { currencies: { EUR: { isDefault: true } } }, FR: { currencies: { EUR: { isDefault: true } } }, JP: { currencies: { JPY: { isDefault: true } } }, RS: { currencies: { RSD: { isDefault: false } } }, ZA: { currencies: { ZAR: { isDefault: true } } } } };
 
<button onclick="getData()">Get Data</button>

Or just in a single step:

function getData() {
    const result = Object
            .keys(myJSON.countries)
            .map(k => Object.keys(myJSON.countries[k].currencies)[0]);

    console.log(result);
}

var myJSON = { countryCode: { Australia: "AU", "United States": "US", Britain: "GB", Japan: "JP", India: "IND", France: "FR", Russia: "RS" }, countries: { AE: { currencies: { AED: { isDefault: true } } }, AL: { currencies: { ALL: { isDefault: true } } }, AU: { currencies: { AUD: { isDefault: true } } }, US: { currencies: { USD: { isDefault: true } } }, GB: { currencies: { EUR: { isDefault: true } } }, FR: { currencies: { EUR: { isDefault: true } } }, JP: { currencies: { JPY: { isDefault: true } } }, RS: { currencies: { RSD: { isDefault: false } } }, ZA: { currencies: { ZAR: { isDefault: true } } } } };
 
<button onclick="getData()">Get Data</button>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You can use map & for..in loop to iterate over the object

var myJSON = {
  "countryCode": {
    "Australia": "AU",
    "United States": "US",
    "Britain": "GB",
    "Japan": "JP",
    "India": "IND",
    "France": "FR",
    "Russia": "RS"
  },
  "countries": {
    "AE": {
      "currencies": {
        "AED": {
          "isDefault": true
        }
      }
    },
    "AL": {
      "currencies": {
        "ALL": {
          "isDefault": true
        }
      }
    },
    "AU": {
      "currencies": {
        "AUD": {
          "isDefault": true
        }
      }
    },
    "US": {
      "currencies": {
        "USD": {
          "isDefault": true
        }
      }
    },
    "GB": {
      "currencies": {
        "EUR": {
          "isDefault": true
        }
      }
    },
    "FR": {
      "currencies": {
        "EUR": {
          "isDefault": true
        }
      }
    },
    "JP": {
      "currencies": {
        "JPY": {
          "isDefault": true
        }
      }
    },
    "RS": {
      "currencies": {
        "RSD": {
          "isDefault": false
        }
      }
    },
    "ZA": {
      "currencies": {
        "ZAR": {
          "isDefault": true
        }
      }
    }
  }
};

function getData() {
  // get countries object
  let getCountries = myJSON.countries;
  // get all country short names in an array
  var ctr = Object.keys(getCountries);
  // iterate that array using map 
  var getCur = ctr.map(function(item) {
    // in countries object get the object where the country shortname
    // matches the object key. Get the curriencies usin for ..in loop
    for (let keys in getCountries[item].currencies) {
      return keys
    }
  })
  console.log(getCur)
}
<button onclick="getData()">Get Data</button>
brk
  • 48,835
  • 10
  • 56
  • 78
  • Thank you :) But, unfortunately, I don't want to use any of the primitive javascript iterators. – Sunny Feb 20 '18 at 15:49
  • Is there any specific reason for this? If you are worried about performance manual for loops are actually conciderably faster, even if slightly less readable. – David Sherman Feb 21 '18 at 06:48
2

One-liner:

result = [].concat(...Object.values(data.countries).map(x => x.currencies).map(Object.keys))

where data is your object

georg
  • 211,518
  • 52
  • 313
  • 390
0

you can simply use:
Object.keys(myJSON.countries).map(con => Object.keys(myJSON.countries[con].currencies));

AZ_
  • 3,094
  • 1
  • 9
  • 19