Jquery:如何从两个数组中创建一个对象数组

时间:2014-12-22 23:35:38

标签: jquery arrays object

我有两个数组:

var last13monthsofData = [0.45, 0.44, 0.46, 0.43, 0.4, 0.41, 0.46, 0.41, 0.4, 0.45, 0.46, 0.46, 0];
var last13monthsofDates = ["11/1/2013", "12/1/2013", "1/1/2014", "2/1/2014", "3/1/2014", "4/1/2014", "5/1/2014", "6/1/2014", "7/1/2014", "8/1/2014", "9/1/2014", "10/1/2014", "11/1/2014"];

我想取这两个数组并创建一个具有相应数据和日期的新对象数组:

the13monthDataDates=[
{"date":"11/1/2013","data":0.45},
{"date":"12/1/2013","data":0.44},
{"date":"1/1/2014","data":0.46},
{"date":"2/1/2014","data":0.43},
{"date":"3/1/2014","data":0.4},...];

2 个答案:

答案 0 :(得分:3)

var arr = [];
for (i = 0; i < last13monthsofData.length; i++) { 
    arr.push({ date: last13monthsofDates[i], data: last13monthsofData[i] });
}

答案 1 :(得分:3)

这应该做:

var the13monthDataDates = $.map(last13monthsofData, function(v,k) {
    return {"date": last13monthsofDates[k], "data":v};
});