Search code examples
javascriptarraysobjectecmascript-6es6-map

ES6 Map to return an array of object keys only


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>


Solution

  • 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>