How to convert an Object {} to an Array [] of key-value pairs in JavaScript

前端 未结 18 1984
名媛妹妹
名媛妹妹 2020-11-22 12:58

I want to convert an object like this:

{\"1\":5,\"2\":7,\"3\":0,\"4\":0,\"5\":0,\"6\":0,\"7\":0,\"8\":0,\"9\":0,\"10\":0,\"11\":0,\"12\":0}

相关标签:
18条回答
  • 2020-11-22 13:13

    Use for in

    var obj = { "10":5, "2":7, "3":0, "4":0, "5":0, "6":0, "7":0,
                "8":0, "9":0, "10":0, "11":0, "12":0 };
    
    var objectToArray = function(obj) {
        var _arr = [];
    
        for (var key in obj) {
            _arr.push([key, obj[key]]);
        }
        return _arr;
    }
    
    console.log(objectToArray(obj));
    
    0 讨论(0)
  • 2020-11-22 13:14

    Use Object.entries to get each element of Object in key & value format, then map through them like this:

    var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
    
    var res = Object.entries(obj).map(([k, v]) => ([Number(k), v]));
    
    console.log(res);

    But, if you are certain that the keys will be in progressive order you can use Object.values and Array#map to do something like this:

    var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}; 
    
                            // idx is the index, you can use any logic to increment it (starts from 0)
    let result = Object.values(obj).map((e, idx) => ([++idx, e]));
    
    console.log(result);

    0 讨论(0)
  • 2020-11-22 13:16

    With lodash, in addition to the answer provided above, you can also have the key in the output array.

    Without the object keys in the output array

    for:

    const array = _.values(obj);
    

    If obj is the following:

    { “art”: { id: 1,  title: “aaaa” }, “fiction”: { id: 22,  title: “7777”} }
    

    Then array will be:

    [ { id: 1, title: “aaaa” }, { id: 22, title: “7777” } ]
    

    With the object keys in the output array

    If you write instead ('genre' is a string that you choose):

    const array= _.map(obj, (val, id) => {
        return { ...val, genre: key };
      });
    

    You will get:

    [ 
      { id: 1, title: “aaaa” , genre: “art”}, 
      { id: 22, title: “7777”, genre: “fiction” }
    ]
    
    0 讨论(0)
  • 2020-11-22 13:16

    You can use Object.values([]), you might need this polyfill if you don't already:

    const objectToValuesPolyfill = (object) => {
      return Object.keys(object).map(key => object[key]);
    };
    Object.values = Object.values || objectToValuesPolyfill;
    

    https://stackoverflow.com/a/54822153/846348

    Then you can just do:

    var object = {1: 'hello', 2: 'world'};
    var array = Object.values(object);
    

    Just remember that arrays in js can only use numerical keys so if you used something else in the object then those will become `0,1,2...x``

    It can be useful to remove duplicates for example if you have a unique key.

    var obj = {};
    object[uniqueKey] = '...';
    
    0 讨论(0)
  • 2020-11-22 13:17

    This is my simple barebone implementation:

    let obj = {
      "1": 5,
      "2": 7,
      "3": 0,
      "4": 0,
      "5": 0,
      "6": 0,
      "7": 0,
      "8": 0,
      "9": 0,
      "10": 0,
      "11": 0,
      "12": 0
    };    
    
    const objectToArray = obj => {
          let sol = [];
          for (key in obj) {
            sol.push([key, obj[key]]);
          }
          return sol;
        };
    
    objectToArray(obj)
    
    0 讨论(0)
  • 2020-11-22 13:18

    We can change Number to String type for Key like below:

    var obj = {"1":5,"2":7,"3":0,"4":0,"5":0,"6":0,"7":0,"8":0,"9":0,"10":0,"11":0,"12":0}
    var result = Object.keys(obj).map(function(key) {
      return [String(key), obj[key]];
    });
        
    console.log(result);

    0 讨论(0)
提交回复
热议问题