0

I have declared an array variable and I want to push new data but it doesn't work. below is my code

let locations = [
  [2.4333,4.45333],
  [3.45454,5.222325]
]

here is the code pushing to locations variable

 for (var j=0;j<res.length;j++) {
    locations.push([res[j]['lat'], res[j]['long']])
    //4.3333, 4.33444
 }

and this is the code to display

for (i = 0; i < locations.length; i++) {
  position: new google.maps.LatLng(locations[i][0], locations[i][1])
}

This is what I'm getting in console log

> Array[]
console.log(locations.length)// 0

Thanks

5
  • please add the error, you get. Commented Feb 18, 2020 at 7:58
  • Print out a res[j]. Commented Feb 18, 2020 at 7:59
  • Can you console.log(res) after the 'code pushing to locations variable' code. Then you will know if it is an issue with adding, or displaying Commented Feb 18, 2020 at 8:00
  • position: new ... is not a JS statement. position = ... ? What do you mean by "doesn't work" ? Commented Feb 18, 2020 at 8:00
  • I've put a console log message. Thanks Commented Feb 18, 2020 at 8:19

2 Answers 2

1

You have used the wrong variable in loop.

for (var j=0;j<res.length;j++) {
    latlong.push([res[j]['lat'], res[j]['long']])
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Where is latlong defined?
@JMadelaine I am assuming he has already used the data structure for latlong in array of objects let res = [{"lat":"112", "long":"123"},{"lat":"112","long":"123"}]
1

With some assumptions.

var res = [{ lat: 1234, long: 4567 }, { lat: 2234, long: 5567 }];
var locations = [
    [2.4333, 4.45333],
    [3.45454, 5.222325]
];
for (var j = 0; j < res.length; j++) {
    locations.push([res[j]['lat'], res[j]['long']])
}
console.log(locations);

1 Comment

Did you check with Run Code Snippet or while running in your dev environment?

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.