Converting Google Sheets data into JavaScript array for Maps integration

I’m working on a WordPress site that displays map pins using data from a Google Sheets document. The setup lets certain users add location details to the spreadsheet, which then shows up as markers on the map.

I can pull the data from Google Sheets, but I’m having trouble formatting it correctly. Here’s my current code:

for (var x = 0; x < data.feed.entry.length; x++) {
    var record = data.feed.entry[x];
    
    var name = record["gsx$name"].$t;
    var longitude = parseFloat(record["gsx$lng"].$t);
    var latitude = parseFloat(record["gsx$lat"].$t);
    var description = record["gsx$desc"].$t;
    var type = record["gsx$type"].$t;
    
    if (latitude != "0" && longitude != "0") {
        var locations = [name, longitude, latitude, description];
    }
}

I need to create a multi-dimensional array where each row becomes its own array. The final result should look like this:

var locations = [
    ['Location A', 33.123456, -96.789012, 'Road work on Main Street between 1st and 2nd Avenue'],
    ['Location B', 33.654321, -96.210987, 'Bridge repair on Highway 75 northbound lanes']
];

How can I modify my code to build this structure?

The issue is with variable scope and array initialization. You’re creating a new locations variable inside each loop iteration instead of building up a single array. Move the array declaration outside the loop and use the push method to add elements. Here’s the corrected approach:

var locations = []; // Initialize empty array before loop

for (var x = 0; x < data.feed.entry.length; x++) {
    var record = data.feed.entry[x];
    
    var name = record["gsx$name"].$t;
    var longitude = parseFloat(record["gsx$lng"].$t);
    var latitude = parseFloat(record["gsx$lat"].$t);
    var description = record["gsx$desc"].$t;
    
    if (latitude != "0" && longitude != "0") {
        locations.push([name, longitude, latitude, description]);
    }
}

This way you’ll accumulate all valid location records into the single multi-dimensional array structure your mapping library expects.

Had the exact same problem when I was building a store locator last year. The key mistake you’re making is reinitializing your array on every loop iteration. What happens is you create a new locations variable each time, so only your last valid record survives. The fix is straightforward but easy to miss when you’re focused on the data parsing logic. Declare your locations = [] before entering the loop, then accumulate records with locations.push(). Also noticed you might want to add some validation for empty strings or invalid coordinates since Google Sheets can have inconsistent data types. I ended up checking for both zero values and empty strings in my condition because users sometimes left cells blank which caused mapping issues later.

you’r declaring the locations array inside the loop which overwrites it each time. try declaring it outside first then push each record into it. should be something like var locations = []; before the loop then locations.push([name, longitude, latitude, description]); inside the if statement