ES6 Map to return an array of object keys only

谁说我不能喝 提交于 2019-12-05 15:25:31

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>

You can use map & for..in loop to iterate over the object

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>

One-liner:

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

where data is your object

you can simply use:
Object.keys(myJSON.countries).map(con => Object.keys(myJSON.countries[con].currencies));

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!