1

我想像这样获得相同的数组:

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"],
        ];

如何组合到数组:项目和值以获得相同的第一个示例?

4

1 回答 1

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);
于 2013-10-23T01:44:46.363 回答