Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Inserting to array from a loop. Need help!

Options
  • 09-12-2013 11:41am
    #1
    Registered Users Posts: 4,946 ✭✭✭


    Hey guys,

    I'm having issues inserting data into an array and I'm wondering if someone could help me with some code?

    I'm reading data from a csv file and 3 of the values are going to be used for populating a map. the values are

    result[1] (name)
    result[6] (lat)
    result[7] (long)

    I need to populate this (psudo)array as follows

    var locations = [
         ['<h4>result[i][1]</h4>', result[i][6], result[i][7]],
         ['<h4>result[i][1]</h4>', result[i][6], result[i][7]],
         ['<h4>result[i][1]</h4>', result[i][6], result[i][7]],
         ['<h4>result[i][1]</h4>', result[i][6], result[i][7]],
    ];
    

    Should I be doing the following? Or am I completely wrong here?
    var x = { [i][1], [i][6], [i][7] }
    locations.push(x);
    

    Cheers


Comments

  • Registered Users Posts: 1,311 ✭✭✭Procasinator


    Probably more like:
    var x = { name: [i][1], lat: [i][6], long: [i][7] }
    locations.push(x);
    

    Or do you want to keep it as a multi-dimensional array? If so, just replace your braces with brackets in your current code.


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Actually using psudo-code here isn't really helping as this question is as much about syntax as it is about anything else.

    Still, returning to what you basically want to do; you're reading data from a CSV file and 3 of the values are going to be inserted into an array for later use. So you want to essentially convert your CSV into a multi-dimentional array, assigning them to positions 1, 6 and 7 in each 'row' of the array.

    For simplicity's sake, I'll presume you're looping through the rows in your CSV and you've now read a row there and assigned the three values to three separate variables; vName, vLat, vLon. I'll presume your locations array 'row' length is 8 and is empty at this stage.

    So as you read each row in the CSV you could assign it thus:
    var tempArray = { null, vName, null, null, null, null, vLat, vLon};
    locations.push(tempArray);
    
    If locations is not empty, and the order of locations matches up with the CSV, then you are simply asigning values to an existing array, going through it using a for loop:
    locations[1] = vName;
    locations[6] = vLat;
    locations[7] = vLon;
    You naturally don't need push, as you're not adding new data, just updating existing data.

    If you're using key-value pairs, then something like what Procasinator suggested might also work, but this is where it comes down to what language you're using and what you're allowed do or not do with arrays in that language.


Advertisement