ES6 Map仅返回对象键数组

时间:2018-02-20 15:15:05

标签: javascript arrays object ecmascript-6 es6-map

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

是的,我主要需要使用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>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:5)

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

myArr1.map((key, value) => Object.keys(key)[0]);

&#13;
&#13;
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 } } } } };
	
&#13;
<button onclick="getData()">Get Data</button>
&#13;
&#13;
&#13;

或者只需一步:

&#13;
&#13;
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 } } } } };
	
&#13;
<button onclick="getData()">Get Data</button>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您可以使用map&amp; for..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>

答案 2 :(得分:1)

一衬垫:

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

其中data是您的对象

答案 3 :(得分:0)

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