0

I have this array:

0: {links: www.example.com, coordinates: 8.99, 5.00}
1: {links: www.exampleB.com, coordinates: 5.99, 2.00}

And on ajax success I need to push each single value to an array, I tried:

success: function(data) {
  coords = [];
  for (var i = 0; i < data.length; i++) {
    coords.push(data.coordinates[i]);
  };
...

But I get:

Uncaught TypeError: Cannot read property '0' of undefined

On this line:

coords.push(data.coordinates[i]);
9
  • 1
    I have this array: What exactly are you logging there? Commented Jul 1, 2019 at 0:52
  • @CertainPerformance what do you mean? I push values to a multidimensional array, and that is the structure I get. I then would like to push each single value to a specific new array Commented Jul 1, 2019 at 0:53
  • 2
    it looks like data is an array so you need to index into it to get at one set of coordinates: data[i].coordinates[j] Commented Jul 1, 2019 at 0:55
  • 1
    Does an array have a coordinates property? Hint - no. An entry may have a coordinates property, but not the array itself... Commented Jul 1, 2019 at 0:56
  • 1
    @MarkMeyer yes indeed, I was doing it the wrong way. Adding the index to coordinates and not data. Tired. Thanks Commented Jul 1, 2019 at 0:57

1 Answer 1

3

You need to access the correct item in data:

coords.push(data[i].coordinates);
Sign up to request clarification or add additional context in comments.

3 Comments

oh thanks a lot, I was doing it the wrong way. Adding the index to coordinates and not data. Will accept it as soon as it let me to.
ctually I now do: for (var i = 0; i < data.length; i++) { coords.push(data[i].coordinates); links.push(data[i].links); }; but that is adding twice a value as I get 0: ["40.7272074, 8.575266499999998"] 1: (2) ["40.7272074, 8.575266499999998", "40.7197406, 8.563512299999957"] I might have to do a array unique unless there is another way
had to do coords.push(data[i].coordinates[i]); links.push(data[i].links[i]);

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.