0

I have a DB with some info in and a function that accesses that info. However, using an old DB I have returned a perfect bidimensional array, so I can use it with no problems at all. Now, with the new DB info, for some reason I am receiving some data in the right way and some other data as an object:

World goes from 0 to 149 and each of them do the same, so I have

0  0 [1,1,1]
0  1 [1,1,1] etc

but in positions 2 and 47, instead of an array of arrays, I have an object:

2: {0 [1,1,1] ...

The whole idea is to put everything into a bidimensional array:

[[1,1,1],[1,1,1],...],[[1,1,1],[1,1,1],...]

I have tried a few things like Array.from but it seems like since it has a mix between it won´t be converted.

What should I do to convert it the right way or to access only those wich are objects, convert them, then add every element in its right position to a bidimensional array?

(I tried to explain it the best way I can... let me know if there is any other explanation you may need to help me out overhere)

Thanks! ps: The info comes from an API

This is what I get:

world

Object { 0=[150],  1=[150],  2={...},  más...}

0

[1, 1, 1, 147 más...]

1

[1, 1, 1, 147 más...]

2

Object { 0=1,  1=1,  2=1,  más...}

3

[1, 1, 1, 147 más...]

4

[1, 1, 1, 147 más...]

5

[1, 1, 1, 147 más...]

6

[1, 1, 1, 147 más...]

7

[1, 1, 1, 147 más...]

8

[1, 1, 1, 147 más...]

9

[1, 1, 1, 147 más...]

10

[1, 1, 1, 147 más...]

This is what I need:

world

[[1, 1, 1, 147 más...], [1, 1, 1, 147 más...], [1, 1, 1, 147 más...], 147 más...]

0

[1, 1, 1, 147 más...]

1

[1, 1, 1, 147 más...]

2

[1, 1, 1, 147 más...]

3

[1, 1, 1, 147 más...]

4

[1, 1, 1, 147 más...]

5

[1, 1, 1, 147 más...]

6

[1, 1, 1, 147 más...]

7

[1, 1, 1, 147 más...]

8

[1, 1, 1, 147 más...]

9

[1, 1, 1, 147 más...]

10

[1, 1, 1, 147 más...]
user3074989
  • 105
  • 2
  • 9
  • If you control the code that produces your array it would be best to fix the problem there - but we can't help with that unless you show the code. If you don't control that code and you are stuck dealing with a not-quite-right structure coming from some other API, we can't help with that either unless you [edit] your question to show the input format clearly. – nnnnnn Aug 17 '16 at 04:02
  • Just added the functions that brings on the problem, since I dont have control on the array itself. I was thinking, maybe, there is a way I can check each element of the object configs.world and if is an array push it into world and, if not, convert it, then push it. Not sure if is possible, though – user3074989 Aug 17 '16 at 04:39
  • I'd like to help you but you are blowing my mind. Can you isolate the problem into a more concise statement. Pretend I am a computer program. You are giving me exactly what input and you are expecting what output? – javaMoca Aug 17 '16 at 04:44
  • Please show the structure of `mapResp.data.grid`. I don't think the rest of your code is all that relevant, because really the core of your problem is "How do I transform input structure X to output structure Y?", but you don't clearly show the input structure. (`console.log(JSON.stringify(mapResp.data.grid))` will produce an easy-to-read format of it that you can edit down a little since we don't need to see all 150 elements, just three or four correct ones and one of the incorrect ones...) – nnnnnn Aug 17 '16 at 04:50
  • Just changed the code for the results (gotten and expected) – user3074989 Aug 17 '16 at 04:57

1 Answers1

0

Ok guys,

I am not sure if this is the best way to do it, but here is how I came up with the desired result:

var testing = [[]];
var test = [];
var numberOfItems = 0;
for(var item in configs.world){
    numberOfItems++;
}
testing.splice(0, testing.length);
for(var i = 0; i < numberOfItems; i++){        
    console.log(i);
    if(Array.isArray(configs.world[i])){
        testing.push(configs.world[i]);
    } else {            
        test.splice(0,test.length);
        for(var key in configs.world[i]){
            var value = configs.world[i][key];
            test.push(value);
        }
        testing.push(test);                 
    }
}
world = testing;
user3074989
  • 105
  • 2
  • 9