0

I have a list which i'm pushing into an array dataList. now I another function i need to pop the contents how will i go about it. Here is the code which i'm trying.

var dataList = new Array;
function addTheContents(){
// .... I'm doing some ajax call and getting one list,
success : function(map) {
                console.log(map);
                dataList.push(map);
                        },
error :  function() {
                alert("error occured!!!");
            },
}

function getListData(){

 dataList.pop()..... how will i get the contents from the datalist after pushing??

}
2
  • make sure dataList holds data before doing pop() operation. FYI: pop() returns the last elements within the dataList. Commented Jan 31, 2014 at 5:55
  • Make sure getListData is called after the Ajax call succeeded. Have a look at stackoverflow.com/questions/14220321/…. Commented Jan 31, 2014 at 5:59

2 Answers 2

3

dataList is an array. You can read the items from the array by iterating over the indexes of the array as in:

for (var i = 0; i < dataList.length; i++) {
    // do something with dataList[i]
}

You can read just the last item in the array:

var item = dataList[dataList.length - 1];
// do something with item here

Or, if you just want to remove and retrieve the last from the array, you can do this:

var item = dataList.pop();
// do something with item here

MDN documentation for .pop() here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop. It's basically the opposite of .push().

There are a zillion other operations you can carry out on an array and they are all listed here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


In your specific code example where you are doing an ajax call, you also need to understand that most ajax calls are asynchronous. That means they finish some time in the future. As such, you can't just call the ajax call and then immediately examine dataList. Instead, you need to put any code that needs to reference dataList in the success handler for the ajax call or you need to call that code from the success handler and then pass dataList to it. This is how asynchronous programming works in javascript.

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

Comments

0

If you want to print all elements from your datalist you can use this code.

$.each(dataList, function(index, val) {
    console.log(val.category);
});

In this code you will get index as key and val as value at that position.

but pop() is used to remove last element from array like.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();

output:- Banana,Orange,Apple

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.