Converting JavaScript object with numeric keys into array

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

    Here is an example of how you could get an array of objects and then sort the array.

      function osort(obj)
      {  // map the object to an array [key, obj[key]]
        return Object.keys(obj).map(function(key) { return [key, obj[key]] }).sort(
          function (keya, keyb)
          { // sort(from largest to smallest)
              return keyb[1] - keya[1];
          }
        );
      }
    

提交回复
热议问题