Currently, I am working on a method to retrieve an array of object keys representing currencies associated with a specific country. However, my current implementation only returns all currency values in an array.
I aim to utilize ES6 methods exclusively without resorting to other iterators.
Example: Desired Output
If the filter is set based on the country code 'US', the expected result should be an array containing ['USD'].
const myArr = Object.keys(myJSON.countries).filter((k) => (Object.keys(myJSON.countries[k].currencies)[0]) where country === 'US');
Output: ['USD']
This request is linked to a previous inquiry -> ES6 Map to return an array of object keys only
Assistance in achieving this goal would be greatly appreciated. Here's the provided code:
const elem = document.getElementById('currencyList');
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
}
}
},
// Rest of JSON data ...
};
function getData() {
const myArr = Object.keys(myJSON.countries).map((k) => (Object.keys(myJSON.countries[k].currencies)[0]));
console.log(myArr);
for(var i=0; i< myArr.length; i++) {
el = document.createElement('option');
el.textContent = myArr[i];
el.value = myArr[i];
elem.appendChild(el);
}
}
<button onclick="getData()">Get Data</button>
<br />
<select id="currencyList">
</select>