Enes GENÇ Enes GENÇ - 3 months ago 6
Javascript Question

reformat a json array

I want to transform this array :

<pre>
result :

[{
"p": 0
},
{
"p": 1
},
{
"p": 2
},
{
"p": 3
}]

</pre>


that's what i want :




{
"p1": "1",
"p2": "2",
"p3": "3",
"p4": "4",
"p5": "5"
}



Example: https://jsfiddle.net/nsgnc0/33hvf4t5/

how to do it ?

Answer
Try this



    var modPar = {};
    for(var i=1; i<5; i++){
         modPar ['p'+i] = i;
    }

    var result = JSON.stringify(modPar);
    console.debug(result);

if you want i as string use

modPar ['p'+i] = i.toString;

here is working fiddle https://jsfiddle.net/jz72vL23/4/