Converting JavaScript object with numeric keys into array

前端 未结 16 2750
-上瘾入骨i
-上瘾入骨i 2020-11-22 03:09

I have an object like this coming back as a JSON response from the server:

{\"0\":\"1\",\"1\":\"2\",\"2\":\"3\",\"3\":\"4\"}

I want to conv

16条回答
  •  猫巷女王i
    2020-11-22 03:22

    Assuming your have a value like the following

    var obj = {"0":"1","1":"2","2":"3","3":"4"};
    

    Then you can turn this into a javascript array using the following

    var arr = [];
    json = JSON.stringify(eval('(' + obj + ')')); //convert to json string
    arr = $.parseJSON(json); //convert to javascript array
    

    This works for converting json into multi-diminsional javascript arrays as well.

    None of the other methods on this page seemed to work completely for me when working with php json-encoded strings except the method I am mentioning herein.

提交回复
热议问题