我有以下数据:
var data = [{
County: "Cork",
date: '1/1/2018',
num: 100
}, {
County: "Roscommon",
date: '07/10/2017',
num: 27
}];
var Counties = {};
for (County in data) {
for (var i = 0; i <= 1; i++) {
Counties[data[i].County] = data[i].County;
}
}
这将返回
{County: "Roscommon"}
我如何获取代码以返回数组对象中的每个县,以便最终得到:
{County: "Cork"}
{County: "Roscommon"}
最佳答案
您可以使用array#map函数。
const countyArray = data.map(el => {el.County})
要么
const counties = [];
for (let county of data) {
counties.push({ County })
}
作为对象:
const counties = {};
for (let element of data) {
counties[element.County] = {County: element.County};
}