0

I want to split my multidimensional array into objects. I have made a array:

var arrayFirstLast = [
                         [58.94142647682763][23.5423357],
                         [59.94142647682765][24.5423357], 
                     ];

I know only how do deal with array like:

var arrayFirstLast=[58.94142647682763],[23.5423357];

and I want:

var arrayLongLat= [
                      {
                          "long":58.94142647682763,
                          "lat":23.5423357
                      },
                      {
                          "long":59.94142647682763,
                          "lat":24.5423357
                      }
                  ];
2
  • 1
    What's the problem? You do know that your last code segment is valid, right? Commented Feb 26, 2015 at 21:33
  • 3
    The array that you say that you have made, doesn't work. It runs, but the result is [undefined, undefined]. Commented Feb 26, 2015 at 21:34

3 Answers 3

1

for the output you want is an array of objects. so for this

var arrayFirstLast=[[58.94142647682763,23.5423357],
              [59.94142647682765,24.5423357]];
var arrayLongLat = [],
    latLon = {};
for(var i in arrayFirstLast){
    latLon = {long: arrayFirstLast[i][0], lat: arrayFirstLast[i][1]};
    arrayLongLat.push(latLon);
}
Sign up to request clarification or add additional context in comments.

8 Comments

The code gives this error: TypeError: arrayFirstLast[i] is undefined (Not my downvote btw)
It has been edited to be in proper format for the multi dimensional array, I thought the OP had the proper format I should have realized it was wrong at the start
Now the result is: [{"long":[58.94142647682763],"lat":[23.5423357]},{"long":[59.94142647682765],"lat":[24.5423357]}].
@alex: The code runs, but it doesn't give the expected result. Each value is wrapped in an array. The op's array contains undefined because an expression like [58.94142647682763][23.5423357] looks for the property "23.5423357" in the array created by the first value, and there is no property with that name. When reading a property that doesn't exist, you get the value undefined.
Sure I can explain. As you can see in the OP the arrays were next to each other in JS that isn't possible anything you need to access from an array needs to be inside of another array. This means unlike Java or more staticly typed languages they have default ways to create multidimensional arrays javascript doesn't. This means you need to insert arrays inside other arrays to acheive it. This is why you need a full array, which contains each array of lat lon pair and each of those are contained within their own array.
|
1

You might want to look at your array, it is written in an incorrect syntax.

So let's take the array:

var arrayFirstLast = 
     [[58.94142647682763, 23.5423357], [59.94142647682765, 24.5423357]];

And then we loop through it and make objects out of it:

for (var i = 0; i < arrayFirstLast.length; i++) {
    var obj = { "long": arrayFirstLast[i][0], "lat": arrayFirstLast[i][1] };
    arrayLongLat.push(obj);
}

Comments

-1

Iterate over array and set new array with objects:

var arrayFirstLast=[[58.94142647682763][23.5423357],
                  [59.94142647682765][24.5423357], 
                 ];
var newA = Array();
for ( var i = 0 ; i < arrayFirstLast.length ; i++ ) 
    newA.push({"long":arrayFirstLast[i][0],"lat":arrayFirstLast[i][1]});

newA Array contains results.

1 Comment

The code gives this error: TypeError: arrayFirstLast[i] is undefined (Not my downvote btw)

Your Answer

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.