1

Why am I not able to load following object Points in to 2 dimensional Array MultiPoints?

 var points = {lat:"48", long:"123"};
    var MultiPoints = [];
    
    for (i = 0; i <3; i++) { 
        MultiPoints.push(points[i].lat, points[i].long );
    }
    
    console.log(MultiPoints);

What I need to have is

var MultiPoints = [ [48, 123], [48, 123], [48, 123] ];

2
  • Read up on JavaScript objects vs. arrays. Not the same thing. Commented Dec 8, 2016 at 21:26
  • lol @Timo that's debatable. Commented Dec 8, 2016 at 21:26

1 Answer 1

1

You do not have an array to iterate, just one object, then you need to push an array as well.

var points = { lat: "48", long: "123" },
    MultiPoints = [],
    i;
    
for (i = 0; i < 3; i++) { 
    MultiPoints.push([points.lat, points.long]);
}
   
console.log(MultiPoints);

A suggestion for mapping multiple points with Array#map.

The map() method creates a new array with the results of calling a provided function on every element in this array.

var points = [{ lat: "48", long: "123" }, { lat: "49", long: "124" }, { lat: "50", long: "125" }], 
    multiPoints = points.map(function (point) {
        return [point.lat, point.long];
    });
   
console.log(multiPoints);

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Nina specially for the suggestion part!
i suggest to use a convention for naming variables and use only names with starting upper case letter for classes.

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.