我想获得相同的数组:
var data =
[
{"date":"20111001","Happy":"63.4","Sad":"62.7","Angry":"72.2"},
{"date":"20111002","Happy":"67.4","Sad":"61.7","Angry":"52.2"},
{"date":"20111003","Happy":"60.4","Sad":"84.7","Angry":"44.2"}
];
如果我有两个列表:列表项
var items=["date","Happy","Sad","Angry"];
和值数组:
var values=[
["20111002","67.4","61.7","52.2"],
["20111003","60.4","84.7","44.2"],
];
如何组合数组:项目和值以获得相同的第一个示例?
答案 0 :(得分:1)
var items = ["date","Happy","Sad","Angry"];
var values = [
["20111002","67.4","61.7","52.2"],
["20111003","60.4","84.7","44.2"],
];
var data = [],
itemLen = items.length,
i, len, j, item, o;
for (i = 0, len = values.length; i < len; ++i) {
item = values[i];
o = {};
for (j = 0; j < itemLen; ++j) {
o[items[j]] = item[j];
}
data.push(o);
}
console.log(data);