繁体   English   中英

ES6 Map仅返回对象键数组

[英]ES6 Map to return an array of object keys only

我正在尝试编写一个方法来帮助我返回所有货币的对象键数组。 但是,我陷入了一个点,我得到了具有键值对的完整对象数组。

是的,我主要需要使用ES6方法。 我不想使用任何其他迭代器。

例如:我需要什么:

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

我得到了什么:

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

你能帮助我实现这个目标吗?

这是代码:

 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> 

您可以获取对象的第一个键。

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> 

或者只需一步:

 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> 

您可以使用mapfor..in循环迭代对象

 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> 

一内胆:

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

data是你的对象

你可以简单地使用:
Object.keys(myJSON.countries).map(con => Object.keys(myJSON.countries [con] .currencies));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM