2

I assign array[0][0] and array[0][1] with predefined variable and rest of the array with the data from php variable with for loop.

function getDistance(){
    var row = JSON.parse(document.getElementById('rows').innerHTML);
    var markers = JSON.parse(document.getElementById('markers').innerHTML);
    var distance = new Array(row);
    for (var i = 0; i < row; i++) {
        distance[i] = new Array(2);
    }
    distance [0][0]= 16.78789;
    distance [0][1]= 96.15291;
    for(var i = 0; i<markers.length; i++){
        var latt = parseFloat(markers[i].latitude);
        var longt = parseFloat(markers[i].longitude);
        distance [i+1][0]= latt;
        distance [i+1][1]= longt;
    }
}

And this is where I get error in distance[i+1]. Its say distance[i+1] is undefined. The code is working fine if i assign distance[i][0] in for loop it means that I assign to distance[0][0] and [0][1]. Its doesn't allow me to start at array index 1. This code is working there is nothing wrong with my variables I get all the variable I want only at array indexing getting error.

function getDistance(){
var row = JSON.parse(document.getElementById('rows').innerHTML);
var markers = JSON.parse(document.getElementById('markers').innerHTML);
var distance = new Array(row);
for (var i = 0; i < row; i++) {
  distance[i] = new Array(2);
}
for(var i = 0; i<markers.length; i++){
    var latt = parseFloat(markers[i].latitude);
    var longt = parseFloat(markers[i].longitude);
    distance [i][0]= latt;
    distance [i][1]= longt;
}
}
3
  • 1
    what is the value of row ... it better be one greater than the value of markers.length - because i+1 Commented Jul 27, 2018 at 3:40
  • 1
    Javascript doesn't support multi-dimensional arrays, so of course the array you try to access does not exist if you didn't create it. did you create the i+1 array before trying to put elements in it? no. Commented Jul 27, 2018 at 3:42
  • I edited my question and its working fine and I also try with marker.length and get same error Commented Jul 27, 2018 at 3:54

2 Answers 2

1

Make sure "row" is a number after You parse that JSON, otherwise you have an array included a single "string" or "object" item at 0

var row = parseInt(JSON.parse(document.getElementById('rows').innerHTML));

and then check "distance" length

var distance = new Array(row);
console.log(distance)
Sign up to request clarification or add additional context in comments.

3 Comments

I edited my question everything is fine in second code.
I checked your code on my pc and I added [{"a" : "1"},{"b" : "2"}] as the innerHtml of markers and rows, then distance[i+1] was undefined , so i changed rows to a fixed number in first loop for (var i = 0; i < row; i++) {...} and problem solved. try this: for (var i = 0; i <= row.length; i++) {...}
It working now i changed for(var i = 1; i < distance.length; i++) and used distance[i][0]....
1

Array is zero index so you should compare to row-1

for (var i = 1; i < distance.length-1; i++) {

And also remove +1 in this lines

distance [i][0]= latt;
distance [i][1]= longt;

6 Comments

if i start loop at var i=1 i can't get values from markers at index 0
You just fill the array here in this code while using it set i to 0
Thank u sir its working now but i need to use distance.length instant of distance.length-1 to get all my data.
Ok make it that but you have to check if i <distance.length if yes do the code else do nothing or return null
Or use while loop instead of for loop
|

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.