1

I currently have this multidimensional array:

var locations = [
    ['Event', 'Monday', 50.820250, -0.143534, 'Image', 'fa fa-heart'],
    ['Event 2', 'Tuesday', 50.819939, -0.140978, 'Image', 'fa fa-heart'], 
];

And I have a JavaScript file with this:

$(function () 
  {
$.ajax({                                      
  url: 'locations.php', data: "", dataType: 'json', success: function(rows)        
  {
    for (var i in rows)
    {
      var row = rows[i];          

      var id = row.id;
      var name = row.name; 
      var date = row.date; 
      var lat = row.lat; 
      var long = row.long;
      var image = row.header;

    var test = "['" + name + "', '" + date + "', " + lat + ", " + long + ", '" + image + "', 'fa fa-heart'],";

    locations.push(test);

          } 

  } 
});

  async: false

});

It's grabbing all the information from the PHP file fine, however I would like to "push" / add a new event into the current array for every loop.

Any help is greatly appreciated!

5
  • test is a string, not an array. Commented Jan 16, 2017 at 19:20
  • Why are you using arrays as the elements of locations? They look like they should be objects. Commented Jan 16, 2017 at 19:21
  • @RyanBoyling I have to ask... I noticed you replied only to one answer, though you didn't address the other (seemingly more concise) answers. Is there a reason the solution provided by either myself for Emmanuel won't work for you? I included an example in my answer that seems to do exactly what you're looking for... Commented Jan 16, 2017 at 21:12
  • I did give a shot at both of your answers, thank you very much for your help! I'm still quite new at all of this so I apologize for not mentioning that earlier. There's a lot of code I didn't share (to make it easier for you to answer) so i'm currently working on developing it still, and yours didn't really fit in although was a perfect piece of code. :-) Commented Jan 16, 2017 at 22:07
  • By excluding code, you're only making it harder for us to answer. All of the answers you've gotten here are correct for the question you've asked. If none of them solved your specific issue, then you either haven't asked the right question, or haven't included enough information. I appreciate your attempt to help, but you can imagine what it's like if I asked you "What color is the sky?" and you say "Blue", and I say "Oh actually there's way more to the question that I decided not to share, so your answer won't work." Commented Jan 17, 2017 at 18:54

3 Answers 3

0

What you are adding is a string - not an array. It should be:

var test = [name, date, lat, long, image, 'fa fa-heart'];

To confirm, before pushing test into the array, print the type. i.e.

console.log(typeof test);

If it's a string, you get 'string' as the output; if it's an array, you get 'object'.

Sign up to request clarification or add additional context in comments.

Comments

0

You're setting your test variable to one long string instead of an actual array. For it to be an array, you can do the following:

var test = [name, date, lat, long, image,'fa fa-heart'];
locations.push(test);

Example: https://jsfiddle.net/ejqzshy5/

Comments

0

You can just create a new array for each row, and then push each item into this array. Finally, push this new array into your locations array.

Here's the JSBin: http://jsbin.com/zumijib/edit?html,css,js,output

for (var i = 0, rowLength = rows.length; i < rowLength; i++) {
  var row = rows[i];
  // create new array
  var newData = [];

  // add new data to end of list.
  newData.push(row.name);
  newData.push(row.date);
  newData.push(row.lat);
  newData.push(row.long);
  newData.push(row.image);
  newData.push('fa fa-heart');

  // add data to current list of locations.
  locations.push(newData);
}

6 Comments

Thanks for the reply and code! It doesn't seem to be pushing it as an array, but instead as a string. I just really need the variable "locations" to end up as ['Event', 'Monday', 50.820250, -0.143534, 'Image', 'fa fa-heart'],['Event 2', 'Monday', 50.820250, -0.143534, 'Image', 'fa fa-heart'],['Event 3', 'Monday', 50.820250, -0.143534, 'Image', 'fa fa-heart'] etc.
Can you console.log() the incoming row data for me? This should work, we are creating a new array to hold the data, adding the data to the array, and then adding this new array to the locations array. It should be an array not a string. It works with the mock data I added to the JSBin: jsbin.com/zumijib/4/edit?js,console
Ah yeah i see it's working. I've just ran into another problem however.. this file is loaded into a different js file, which locations array is then separated. I think my problem is that we're creating the locations array but not displaying the final variable. How could I possibly display var locations = [[stuff]]; at the end of the code?
How are files separated? Like, are you using IIFE/modules patterns to make your code not be in the global scope? If so, then you could export the locations array. Otherwise, to share code that is inside two different scripts like this you'd have to have something in the global scope, like inside your other script do: window.APP = window.APP || {};, window.APP.data = {}; and then window.APP.data.locations = [...];. This would give you a global variable that is a bit discrete, and then you can push your newData array into window.APP.data.locations.
Ah perfect, we're getting somewhere! I'm using $.getScript("js/locations.js", function(){ to load that js file at the moment and it's still not grabbing the window.APP.data. Any idea?
|

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.