我有以下JSON数据:
jobHistoryJsonLst = [{
"uuid" : "bGd_AAABNaMAAAFQHvY0UyTa",
"startDate" : "2015-10-01 15:22:21",
"endDate" : "2015-10-01 15:22:24",
"executionTime" : "0:0:2.951"
}, {
"uuid" : "lat_AAABqh4AAAFPQ8k0U_qu",
"startDate" : "2015-09-23 10:50:02",
"endDate" : "2015-09-23 10:50:06",
"executionTime" : "0:0:3.284"
}]
我想从上面的JSON对象创建两个不同的数组。一个应包含所有startDate数据,另一个应包含所有executionTime数据。
预期输出:
startDateArr = ['2015-10-01 15:22:21', '2015-09-23 10:50:02']
execTimeArr = ['0:0:2.951', '0:0:3.284']
答案 0 :(得分:1)
您可以使用.map
或简单循环,例如
var jobHistoryJsonLst = [{
"uuid" : "bGd_AAABNaMAAAFQHvY0UyTa",
"startDate" : "2015-10-01 15:22:21",
"endDate" : "2015-10-01 15:22:24",
"executionTime" : "0:0:2.951"
}, {
"uuid" : "lat_AAABqh4AAAFPQ8k0U_qu",
"startDate" : "2015-09-23 10:50:02",
"endDate" : "2015-09-23 10:50:06",
"executionTime" : "0:0:3.284"
}];
var dates = [],
times = [],
len = jobHistoryJsonLst.length, i;
for (i = 0; i < len; i++) {
dates.push(jobHistoryJsonLst[i].endDate);
times.push(jobHistoryJsonLst[i].executionTime);
}
console.log(dates, times);
// or with .map
var dates = jobHistoryJsonLst.map(function (el) {
return el.endDate;
});
var times = jobHistoryJsonLst.map(function (el) {
return el.executionTime;
})
console.log(dates, times);
答案 1 :(得分:1)
您可以使用map()表示处理它两次或使用forEach一次循环并自行将其推送到数组。
jobHistoryJsonLst = [{
"uuid" : "bGd_AAABNaMAAAFQHvY0UyTa",
"startDate" : "2015-10-01 15:22:21",
"endDate" : "2015-10-01 15:22:24",
"executionTime" : "0:0:2.951"
}, {
"uuid" : "lat_AAABqh4AAAFPQ8k0U_qu",
"startDate" : "2015-09-23 10:50:02",
"endDate" : "2015-09-23 10:50:06",
"executionTime" : "0:0:3.284"
}];
var start = jobHistoryJsonLst.map( function (obj) { return obj.startDate; } );
var end = jobHistoryJsonLst.map( function (obj) { return obj.endDate; } );
//or
var start = [],
end = [];
jobHistoryJsonLst.forEach( function(obj) {
start.push(obj.startDate);
end.push(obj.endDate);
});