创建从对象属性中提取数据的新javascript数组

时间:2015-02-23 16:18:00

标签: javascript arrays

我需要将类似数据保存在单独属性中的平面数组转换为数组数组,这些数组将每个属性的数据字段组合在一起。希望一个例子可以理解这一点。

原始数组:

        [{"MtmDate":"2014-10-24", "MouldingID":"1233B", "ID":38, "Mtm01":5.10, "Mtm02":6.63,"Mtm03":5.84 },
         {"MtmDate":"2014-10-25", "MouldingID":"1233B", "ID":39, "Mtm01":5.13, "Mtm02":6.21,"Mtm03":6.64 },
         {"MtmDate":"2014-10-26", "MouldingID":"1233B", "ID":40, "Mtm01":5.68, "Mtm02":5.95,"Mtm03":6.37 },
...
]

需要输出:

    [{"Mtm": 1, values: [{"MtmDate": "2014-10-24", "ID":38, data: 5.10},
                       {"MtmDate": "2014-10-25", "ID":39, data: 5.13},
                       {"MtmDate": "2014-10-26", "ID":40, data: 5.68}]},
     {"Mtm": 2, values: [{"MtmDate": "2014-10-24", "ID":38, data: 6.63},
                       {"MtmDate": "2014-10-25", "ID":39, data: 6.21},
                       {"MtmDate": "2014-10-26", "ID":40, data: 5.95}]},
     {"Mtm": 3, values: [{"MtmDate": "2014-10-24", "ID":38, data: 5.84},
                       {"MtmDate": "2014-10-25", "ID":39, data: 6.64},
                       {"MtmDate": "2014-10-26", "ID":40, data: 6.37}]},
     ]

我知道我可以通过“手动”为每个数据字段设置对象并将它们添加到新数组来实现此目的:

    var mtm1 = { mtm: 1, values: [] }, mtm2 = { mtm: 2, values: [] }, mtm3 = { mtm: 3, values: [] };
    var dataByPoint = [];
    dataByPoint.push(mtm1);
    dataByPoint.push(mtm2);
    dataByPoint.push(mtm3);

然后遍历原始数组,抓取每个对象的数据:

$.each(d, function(index, value) {
    var details1 = { mtmDate: value.MtmDate, id: value.ID, data: value.Mtm01 };
    if (details1.data) dataByPoint[0].values.push(details1);

    var details2 = { mtmDate: value.MtmDate, id: value.ID, data: value.Mtm02 };
    if (details2.data) dataByPoint[1].values.push(details2);

    var details3 = { mtmDate: value.MtmDate, id: value.ID, data: value.Mtm03 };
    if (details3.data) dataByPoint[2].values.push(details3); 
});

然而实际上有十几个这样的数据属性,这种方法似乎啰嗦。有没有办法通过循环数据属性来填充新数组?

2 个答案:

答案 0 :(得分:1)

使用数组的map函数。

var originalArray =   [{"MtmDate":"2014-10-24", "MouldingID":"1233B", "ID":38, "Mtm01":5.10, "Mtm02":6.63,"Mtm03":5.84 },
         {"MtmDate":"2014-10-25", "MouldingID":"1233B", "ID":39, "Mtm01":5.13, "Mtm02":6.21,"Mtm03":6.64 },
         {"MtmDate":"2014-10-26", "MouldingID":"1233B", "ID":40, "Mtm01":5.68, "Mtm02":5.95,"Mtm03":6.37 },
...
]

var expectedArray = originalArray.map(function(v){
   return {mtm:v.ID, values:v}
})

答案 1 :(得分:1)

var arr =[{"MtmDate":"2014-10-24", "MouldingID":"1233B", "ID":38, "Mtm01":5.10, "Mtm02":6.63,"Mtm03":5.84 },
         {"MtmDate":"2014-10-25", "MouldingID":"1233B", "ID":39, "Mtm01":5.13, "Mtm02":6.21,"Mtm03":6.64 },
         {"MtmDate":"2014-10-26", "MouldingID":"1233B", "ID":40, "Mtm01":5.68, "Mtm02":5.95,"Mtm03":6.37 }
		]
		
var newArr = [];		
	for (var i = 0; i < arr.length; i++)
        {
	    var obj = {};
	    obj.Mtm = (i+1);
	    obj.values = [];
        if (Object.keys(arr[i])[i+3]) //test if the `Mtm##` is present.
        {
              for (var c = 0; c < arr.length; c++)
              {
                   var keys = Object.keys(arr[c]);		
	       	   obj.values.push({"MtmDate" : arr[c].MtmDate, "ID" : arr[c].ID, "data" :  arr[c][keys[i+3]]}); 
	       }
        }
	    newArr.push(obj);
	}

document.write(JSON.stringify(newArr))

这应该这样做。它使用两个循环来解析数据。

外部循环在新数组中创建单个条目,而内部循环解析原始元素中的所有条目,内容以正确的方式分割。 Object.keys用于根据循环索引选择正确的数据元素。