Converting JavaScript object with numeric keys into array

前端 未结 16 2652
-上瘾入骨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条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 03:39

    var obj = {"0":"1","1":"2","2":"3","3":"4"};
    
    var vals = Object.values(obj);
    
    console.log(vals); //["1", "2", "3", "4"]
    

    Another alternative to the question

    var vals = Object.values(JSON.parse(obj)); //where json needs to be parsed
    

提交回复
热议问题