Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 813
Sunny Avatar asked Feb 20 '18 15:02

Sunny


People also ask

How do I apply a map to an array of objects?

The syntax for the map() method is as follows: arr. map(function(element, index, array){ }, this); The callback function() is called on each array element, and the map() method always passes the current element , the index of the current element, and the whole array object to it.

Does object keys return an array?

Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object . The ordering of the properties is the same as that given by looping over the properties of the object manually.

How do you access the key of an array of objects?

For getting all of the keys of an Object you can use Object. keys() . Object. keys() takes an object as an argument and returns an array of all the keys.

How do I return an array in ES6?

Look for item starting from position pos, return the index or -1 if not found. This method create new array by calling the provided function in every element. This create array from every arguments passed into it. Removes the last element from an array and returns that element.


1 Answers

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>
like image 160
Nina Scholz Avatar answered Sep 28 '22 16:09

Nina Scholz