ES6 Map to return an array of object keys only

2019-05-06 16:13发布

问题:

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>

回答1:

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>



回答2:

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>



回答3:

One-liner:

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

where data is your object



回答4:

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