0

我有一个格式为 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

4 回答 4

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;
});
于 2012-10-18T06:49:18.973 回答
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);
}
于 2012-10-18T06:42:07.803 回答
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);
}
于 2012-10-18T06:54:07.583 回答
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" }
]

你可以在这里检查小提琴

于 2012-10-18T07:12:45.940 回答