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>
You could take the first key of the objects.
Or just in a single step:
You can use
map
&for..in
loop to iterate over the objectOne-liner:
where
data
is your objectyou can simply use:
Object.keys(myJSON.countries).map(con => Object.keys(myJSON.countries[con].currencies));