1

From all the answers I've been given, I've decided to use the object reference variable since I need .data to have the same name. However I'm not sure if I am using the variable correctly. I think I just need to do the following:

                      var myData = [];


                    $.ajax({
                            url: url,
                            dataType: "json",
                            cache: true,
                            success: function(data) {           
                            $.each(data, function(i, item){
                      myData.push(item);

                      });
                       alert(myData);  

Unfortunately its still not working correctly..

when I call the function onload i get the following output from the alert 10, " [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"

when I call the function a second time to get the next 10, I should have 20 objects in the file but instead I have only 10 "object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"

ORIGINAL POST BELOW

Good day,

I am trying to add to some results from an Ajax call. Currently json gives me 10 items. When I call my loading function again, I want another 10 but I want to add it to the .data object so I get 20, then when it loads again 30, etc.
Is this possible to do?

The first call gives me back 10 results. It is:

$.ajax({
    url: url,
    dataType: "json",
    cache: true,
    success: function(data) {           

    }
});

I have set the following conditions for the second call.

if(data ===! null || data ===! 0 || data ===! ""){          
    $.each(data, function(i, item){
        data.push(item.text);
    });

The second call is not adding to data at all. I'm not sure what I should do. I have read from other questions that data is an object that holds an array. Why can't I just push 10 more to the array? Any help is appreciated.

Thank you

latoyale
  • 47
  • 2
  • 11

1 Answers1

1

You need to create an object to store the data into. The data that you are fetching each time is not the same object, so that is why you aren't seeing the additions. What you may want to consider is having just a single function, and have it behave like this:

myData = [];

$.ajax({
    url: url,
    dataType: "json",
    cache: true,
    success: function(data) {           
        $.each(data, function(i, item) {
            myData.push(item.text);
        });
    }
});
seeming.amusing
  • 1,179
  • 1
  • 8
  • 18
  • thanks I never considered that I was not getting the same object. I am modifying code that references "data" for everything. Once I do create the array myData, how can I assign its contents to data? – latoyale Mar 20 '12 at 06:45
  • Can I use myData.push(data); or myData.append(data) ? – latoyale Mar 20 '12 at 06:46
  • `.push()` quick and easy way you can add a new array item to the end of a Javascript array object. `.append()` is adding a new DOM object within the DOM structure in jQuery, which isn't what you want. – seeming.amusing Mar 20 '12 at 07:02
  • If you're trying to use `.push()` on an object, it won't work as it is not an array. Therefore, if you set the variable as an object (`myData = {};`), then you will get an error. You will need to set it as an array (`myData = [];`). Additionally, as a note, your updated code may not work because you are using the same variable name `data` in various scopes. That will definitely cause you issues. – seeming.amusing Mar 20 '12 at 07:22
  • yes I noticed. I am definitely having some issues with scopes now. I have updated my code with my updates. However I am still not getting .data to give me the extra 10 items. Not sure what I am doing wrong... – latoyale Mar 20 '12 at 07:30
  • I suspect that you have that full code within a function (either `$(document).ready()` or a `.bind("click")`. If that is the case, you are probably calling `myVar = [];` each time, which effectively resets your array time you execute your code. You should declare `myVar` outside of your function's scope (such as a global variable). – seeming.amusing Mar 20 '12 at 07:50
  • You were right. myData is now collecting all the info for me. Last question... how can I push myData into data? Since so much of the other code depends on data, I need that to update. I can't use push since its an object, so what can i use? – latoyale Mar 20 '12 at 08:04
  • That would be starting to get into a whole other discussion itself, but in short, if `myData` must be an object, you can always create an array as a property of `myData`. I would recommend doing a bit more reading about Javascript objects and arrays. – seeming.amusing Mar 20 '12 at 08:42