1

I want to define a two dimensional array object with for loop... my problem I think my object didn't really get processed, here's the code :

var newLoc = [];

var index;

for (index = 0, i < locations.length; i++){
                if(i == 0) {
                    newLoc[i][0] = locations[i][1];
                    newLoc[i][1] = locations[i][2];
                }
                else {
                    if(locations[i][8] == locations[i-1][8]){
                        newLoc[i-1][0] = (locations[i][1] + locations[i-1][1])/2;
                        newLoc[i-1][1] = (locations[i][2] + locations[i-2][1])/2;    
                    }                            
                    else{
                        newLoc[i][0] = locations[i][1];
                        newLoc[i][1] = locations[i][2];
                    }
                }
            }

locations array itself is the old array which store the data for new array (newLoc). locations' data are existing which are coordinate latitude and longitude. I suppose there is something wrong with my for loop form or how I declare newLoc 2 dimension array, but I still don't know how to fix it. Any help appreciated.

3
  • 1
    for (index = 0, i < locations.length; i++){ should be for (var i = 0; i < locations.length; i++){ right? ( note the ;) Commented Nov 1, 2016 at 11:14
  • 1
    you declared variable index but in for loop used i for iteration. should be: var i; for(i = 0; i < locations.length; i++) {...}. and i can't understand how it should works. Commented Nov 1, 2016 at 11:21
  • thanks to point those out :) Commented Nov 1, 2016 at 11:29

4 Answers 4

2

There are some things you can do with your code for optimization. First you have to initialize your loop correctly. then inside the loop, it is better to assign the value statically rather checking it every time for only one implementation. This should optimize your code. The old location posting could make the following code more optimized.

var newLoc = [];

if(locations.length > 0){
    for(var j = 0; j < 1; ++j) {
        newLoc[j] = [ ];

        newLoc[j][0] = locations[0][1]; 
        newLoc[j][1] = locations[0][2];

    }

}

for (var i = 1, i < locations.length; i++){

    if(locations[i][8] == locations[i-1][8]){
        newLoc[i-1][0] = (locations[i][1] + locations[i-1][1])/2;
        newLoc[i-1][1] = (locations[i][2] + locations[i-2][1])/2;    
    }                            
    else{
        newLoc[i][0] = locations[i][1];
        newLoc[i][1] = locations[i][2];
    }                
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, but do you know how to check if newLoc's value?
In the same way as the location's value is checked. I guess it will be like if(locations.length > 0)
In the same way as the location's value is checked. I guess it will be like if(newLoc.length > 0) –
hahaha...sorry i did not understand. just use console.log(newLoc) for checking it in the console. Dont forget to upvote me :P Good Day..:)
defining var newLoc = []; will throw an error on newLoc[0][0]. There's nothing at index 0
|
1

I think the issue is that newLoc is always a 1 dimentionnal array and you declare 'index' in your for loop but use 'i' in the body

var newLoc = [];

// loop with 
for (var i = 0; i < locations.length; i++){
    //Create the second dimention
    newLoc[i] = [];
            if(i == 0) {
                    ...

1 Comment

okay, still not working.... anyone know how to see the value of newLoc? Check it if the value already defined in the array
0

One thing I noticed, in your for loop, you have written

for (index = 0, i < locations.length; i++)

instead of

for (index = 0; i < locations.length; i++)

Note the ;

So, in all the loop should be

for (index = 0; i < locations.length; i++){
            if(i == 0) {
                newLoc[i][0] = locations[i][1];
                newLoc[i][1] = locations[i][2];
            }
            else {
                if(locations[i][8] == locations[i-1][8]){
                    newLoc[i-1][0] = (locations[i][1] + locations[i-1][1])/2;
                    newLoc[i-1][1] = (locations[i][2] + locations[i-2][1])/2;    
                }                            
                else{
                    newLoc[i][0] = locations[i][1];
                    newLoc[i][1] = locations[i][2];
                }
            }
        }

Comments

0

You are initializing a one-dimensional array, so, when you iterate over the loop, your javascript code will probably you break.

Try to initialize using this way:

var newLoc = new Array(locations.length).fill(locations.length);

Array Doc: https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array

Comments

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.