admin管理员组文章数量:1287584
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?
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?
Share Improve this question edited Jul 25, 2016 at 12:18 Forivin asked Jul 25, 2016 at 12:08 ForivinForivin 15.5k30 gold badges118 silver badges211 bronze badges 7- Can you explain what you are trying to achieve? – Rajesh Commented Jul 25, 2016 at 12:12
-
1
This seems like a very bad structure, before and after. Dynamic object keys are mostly hard to use. Why don't you use something cleaner like for example
[ {player:'player1', numberOfX:10}, {player:'player2', numberOfX:5} ]
. – str Commented Jul 25, 2016 at 12:13 - The structure is a bit more plicated in reality. There is more than just players. What I want to achive is to simply convert a 2d array into an object that uses the array indexes as keys. So that I can get rid of all the null values of the array. – Forivin Commented Jul 25, 2016 at 12:15
- technically it is [10] would be index = 11 – epascarello Commented Jul 25, 2016 at 12:15
- @Forivin If your structure is even more plicated, my point is even more appropriate. – str Commented Jul 25, 2016 at 12:17
3 Answers
Reset to default 6You 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!
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);
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.
本文标签: javascriptConvert multidimensional array to objectStack Overflow
版权声明:本文标题:javascript - Convert multidimensional array to object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741310788a2371636.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论