Best way to convert string to array of object in javascript?

后端 未结 6 1290
萌比男神i
萌比男神i 2020-11-27 19:21

I want to convert below string to an array in javascript.

{a:12, b:c, foo:bar}

How do I convert this string into array of objects? Any cool

6条回答
  •  孤街浪徒
    2020-11-27 19:36

    JSON.parse will do the trick. Once parsed, you can push them into the array.

    var object = JSON.parse(param);
    var array = [];
    for(var i in object) {
       array.push(object[i]);
    }
    

提交回复
热议问题