Convert multidimensional array to object

北慕城南 提交于 2019-12-31 04:46:07

问题


I have a 2d array with up to [32][32] entries. And I'd like to convert it from something like this:

[
    null, 
    null, 
    null, 
    null, 
    null, 
    null, 
    [null, null, null, null, null, null, null, null, null, null, "player1"],
    [null, null, null, null, null, "player2"]
]

to

{
  "6": {"10":"player1"},
  "7": {"5":"player2"}
}

So this would be my array:

var gameField = [];
gameField[6] = [];
gameField[6][10] = "player1";
gameField[7] = [];
gameField[7][5] = "player2";

Now I tried to use this:

var obj = {}
obj = Object.assign({},gameField);
console.log(JSON.stringify(obj));

but it only worked for the outer array, the inner arrays were not affected:

{
    "6": [null, null, null, null, null, null, null, null, null, null, "player1"],
    "7": [null, null, null, null, null, "player2"]
}

What would be the shortest way to do this properly?


回答1:


You can iterate over the items in the array and then recurse if the located item is itself an array (check using Array.isArray)

function populateFromArray(array) {
  var output = {};
  array.forEach(function(item, index) {
    if (!item) return;
    if (Array.isArray(item)) {
      output[index] = populateFromArray(item);
    } else {
      output[index] = item;
    }
  });
  return output;
}

console.log(populateFromArray(input));

This results in:

[object Object] {
  6: [object Object] {
    10: "player1"
  },
  7: [object Object] {
    5: "player2"
  }
}

See a working jsBin

Note: you can certainly do this in less code but less code is not always better!




回答2:


Array.prototype.reduce() seems ideal for this. You may do as follows;

var dataArr = [null,null,null,null,null,null,[null,null,null,null,null,null,null,null,null,null,"Player1"],[null,null,null,null,null,"player2"]],
    dataObj = dataArr.reduce((p,c,i) => (Array.isArray(c) && (p[i] = {[c.length-1]:c[c.length-1]}),p),{});
console.log(dataObj);



回答3:


You could use this recursive function, using ES6 code:

var data = [null,null,null,null,null,null,[null,null,null,null,null,null,null,null,null,null,"player1"],[null,null,null,null,null,"player2"]];

function convert(data) {
    return Array.isArray(data)
        ? data.reduce( (obj, el, i) => (el && (obj[i] = convert(el)), obj), {} )
        : data;
}

var obj = convert(data);

console.log(obj);

This will also work when your input array is nested deeper than 2 levels. It does not require the non-null elements to be at the end of their (sub-)array, nor that there is only one non-null element per (sub-)array.



来源:https://stackoverflow.com/questions/38567371/convert-multidimensional-array-to-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!