1

I basically want to do this in reverse: Convert multidimensional array to object

So let's say I have an object like this:

{
 "6": {"10":{'id':0,'name':'player1'}},
 "7": {"5":{'id':1,'name':'player2'}}
}

How can I convert that into a legit array like this:

[
 null, 
 null, 
 null, 
 null, 
 null, 
 null, 
 [null, null, null, null, null, null, null, null, null, null, {'id':0,'name':'player1'}],
 [null, null, null, null, null, {'id':1,'name':'player2'}]
]

This is the code that I successfully used to convert it the other way around:

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));
asked Jul 25, 2016 at 13:01
5
  • Numbers as attribute names isn't generally a greatest idea. Commented Jul 25, 2016 at 13:06
  • 1
    Technically, it already is a legitimate array. obj[7][5] returns {id: 1, name: "player2"}. The only difference, I guess, is that instead of null all unregistered array positions are undefined. Commented Jul 25, 2016 at 13:06
  • Well I want to loop through the thing without getting errors or checking for undefined. Atm obj[0][0] would through an error, wouldn't it? Commented Jul 25, 2016 at 13:08
  • @Forivin: Yes, because obj[0] is undefined, so it has no first element. Commented Jul 25, 2016 at 13:09
  • obj[0][0] would also throw an error with your new structure Commented Jul 25, 2016 at 13:11

3 Answers 3

1
function objectToArray(obj) {
 var len = Math.max.apply(null, Object.keys(obj));
 if (len !== len) return obj; // checking if NaN
 var output = [];
 for (var i = 0; i <= len; i++) {
 output[i] = null;
 if (obj[i]) { 
 output[i] = objectToArray(obj[i]);
 }
 }
 return output;
}
answered Jul 25, 2016 at 13:20
Sign up to request clarification or add additional context in comments.

Comments

1
var obj = {
 "6": {"10":{'id':0,'name':'player1'}},
 "7": {"5":{'id':1,'name':'player2'}}
};
function populateArray(obj) {
 var range = 0,
 arr = [];
 for (var index in obj) {
 range = Math.max(parseInt(index), range); 
 }
 for (var i = 0; i <= range; i++) {
 if (obj[i+'']) {
 arr[i] = populateArray(obj[i+'']);
 } else {
 arr[i] = null;
 }
 }
 return arr;
}
console.log(populateArray(obj))
answered Jul 25, 2016 at 13:18

Comments

1

Based on your other question, assuming the source is json, you can use a revive function as well to convert the data directly when parsing

var json = '{ "6": {"10":{"id":0, "name":"player1"}}, "7": {"5":{"id":1,"name":"player2"}}}';
function reviver(key,val){	
	if(isNaN(key) || val.id !== undefined)
 	return val;
 var res = [];
 for(var p in val)
 res[p] = val[p];
 return res;
}
var gameField = JSON.parse(json, reviver);
console.log(gameField);

answered Jul 25, 2016 at 14:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.