Converting JavaScript object with numeric keys into array

前端 未结 16 2434
-上瘾入骨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:43

    Not sure what I am missing here but simply trying the below code does the work. Am I missing anything here?

    https://jsfiddle.net/vatsalpande/w3ew5bhq/

    $(document).ready(function(){
    
    var json = {
       "code" :"1", 
       "data" : { 
        "0" : {"id":"1","score":"44"},
        "1" : {"id":"1","score":"44"}
        }
      };
    
      createUpdatedJson();
    
      function createUpdatedJson(){
    
        var updatedJson = json;
    
        updatedJson.data = [updatedJson.data];
    
        $('#jsondata').html(JSON.stringify(updatedJson));
    
    
        console.log(JSON.stringify(updatedJson));
      }
     })
    

提交回复
热议问题