我想转换这个数组:
<pre>
result :
[{
"p": 0
},
{
"p": 1
},
{
"p": 2
},
{
"p": 3
}]
</pre>
这就是我想要的:
{ "p1": "1", "p2": "2", "p3": "3", "p4": "4", "p5": "5" }
示例:https://jsfiddle.net/nsgnc0/33hvf4t5/
怎么做?
答案 0 :(得分:1)
答案 1 :(得分:0)
Try this
var modPar = {};
for(var i=1; i<5; i++){
modPar ['p'+i] = i;
}
var result = JSON.stringify(modPar);
console.debug(result);
如果你想让我作为字符串使用
modPar ['p'+i] = i.toString;
答案 2 :(得分:0)
怎么样:
var orig = JSON.parse("[{\"p\":0},{\"p\":1},{\"p\":2},{\"p\":3}]");
var res = {};
for(var i=1; i<=orig.length; i++){
res["p"+i] = orig[i-1].p;
}
alert(JSON.stringify(res));
&#13;