将对象转换为对象数组

时间:2012-10-18 06:21:47

标签: javascript jquery

我有一个格式为

的JavaScript对象
{
    "header": {
        "dataFields": ["number", "name", "quantity", "id", "from"]
    },
    "data": [
        "20|sam|12|2|2012-06-29T00:00Z|",
        "18|peter|231|12|",
    ]
}

我想尝试这种格式:

[{"number" : "20", "name":"sam", "quantity" : "12", "id":"2"},
 {"number" : "18", "name":"peter", "quantity": "231", "id" 12"}]

我不希望字段“来自输出数组......什么可能是实现它的最好方法?”

var l={};

for ( var key in responseData.positions[i]){
    l.name=key;
        for(var k=0; k<responseData.positions.length;k++){
            for ( var key in responseData.positions[k]) {
                l.value= responseData.positions[k][key] ;
            }
        }
mainArray.push(l);

4 个答案:

答案 0 :(得分:3)

您可以使用数组映射方法迭代数据数组并进行转换。

代码和现场演示:http://jsfiddle.net/N9QyQ/

var object={
"header": {
    "dataFields": ["number", "name", "quantity", "id", "rom"]
},
"data": [
    "20|sam|12|2|2012-06-29T00:00Z",
    "18|peter|231|12|"
 ]
} ;
var result=object.data.map(function(string){
    var split=string.split("|"), item={};
    for (var i=0;i<4;i++) {
        item[object.header.dataFields[i]]=split[i];
    }
    return item;
});

答案 1 :(得分:1)

mainArray = [];
for (i = 0; i < responseData.positions.data.length; i++) {
    var dataArray = responseData.positions.data[i].split("|");
    var newObject = {};
    for (j = 0; j < responseData.positions.header.length; j++) {
        if (responseData.positions.header[j] != "from") {
            newObject[responseData.positions.header[j]] = dataArray[j];
        }
    }
    mainArray.push(newObject);
}

答案 2 :(得分:0)

var obj = {
    "header": {
        "dataFields": ["number", "name", "quantity", "id", "from"]
    },
    "data": [
        "20|sam|12|2|2012-06-29T00:00Z|",
        "18|peter|231|12|"
    ]
};
var result = [];
for(var i = 0, headers = obj.header.dataFields, len = obj.data.length, hLen = headers.length; i < len; i++) {
    var split = obj.data[i].split("|");
    var entry = {};
    // assuming you want first four elements
    /*
    for(var j = 0; j < 4; j++) {
        entry[headers[j]] = split[j];
    }
    */
    // assuming you want to exclude from field
    for(var j = 0; j < hLen; j++) {
        if(headers[j] == "from")
            continue;
        entry[headers[j]] = split[j];
    }       
    result.push(entry);
}

答案 3 :(得分:0)

如果您想使用jQuery,可以执行以下操作

jsonData = {
    "header": {
        "dataFields": ["number", "name", "quantity", "id", "from"]
    },
    "data": [
        "20|sam|12|2|2012-06-29T00:00Z|",
        "18|peter|231|12|",
    ]
};


tmpData = {}
newData = []

$.each(jsonData['data'], function () {
    data = this;
    $.each(jsonData['header']['dataFields'], function (index, header) {
        if (header !== "from") {
            tmpData[header] = data.split('|')[index]
        }
    });
    newData.push(tmpData);
    tmpData = {};
});

newData将包含

[
    {id: "2", name: "sam",number: "20",quantity: "12"},
    { id: "12", name: "peter", number: "18", quantity: "231" }
]

您可以查看小提琴here