Da Silva
Da Silva

Reputation: 229

Converting an object into an array of objects

I have a JavaScript object in the format

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

I am trying to get it in to this format:

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

I dont want the field "from in the output array.. what could be the best way to acheive it??

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);

Upvotes: 0

Views: 405

Answers (4)

Jacob George
Jacob George

Reputation: 2627

If you want to use jQuery you could do the following

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 will contain

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

You can check the fiddle here

Upvotes: 0

cbayram
cbayram

Reputation: 2259

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);
}

Upvotes: 0

Christophe
Christophe

Reputation: 28114

You can use the array map method to iterate through the data array and convert it.

Code and live demo: 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;
});

Upvotes: 3

Barmar
Barmar

Reputation: 780889

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);
}

Upvotes: 1

Related Questions