How to remove the first element of an array using jquery?

I repeat the json dataset, but I need to remove the first element before iteration. How to remove the original item? This is what I still have:

$.post('player_data.php', {method: 'getplayers', params: $('#players_search_form').serialize()}, function(data) { if (data.success) { // How do I remove the first element ? $.each(data.urls, function() { ... }); } }, "json"); 
+8
javascript jquery
source share
7 answers

Normal javascript will do:

 data.urls.shift() 

In shift() method:

Removes the first element from the array and returns this element. This method changes the length of the array.

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/shift

+23
source share

There is a method similar to pop, but from the front instead

 data.urls.shift() 

https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/shift

+2
source share

If data.url is just an array, the easiest way to solve this problem is to use the splice () function in Javascript:

 if (data.success) { //remove the first element from the urls array data.urls.splice(0,1); $.each(data.urls, function() { ... 

You can also use shift () if you need the value of the first URL:

 if (data.success) { //remove the first element from the urls array var firstUrl = data.urls.shift(); //use the value of firstUrl ... $.each(data.urls, function() { ... 
+2
source share

you can use

 .shift() 

It will remove the first element from the array.

In your case, this is

 data.urls.shift() 
0
source share

You can use the shift() function from the Array class, which removes (and returns - although you can just drop it) the first element from the array. See the MDN doc for more information.

 data.urls.shift(); 
0
source share

In JavaScript, you can remove the first element of an array using shift() :

 // your array data.urls; // remove first element var firstElem = data.urls.shift(); // do something with the rest 
0
source share

To remove the first element of an array in javascript (it will also work if you use jquery) use data.urls.shift() . This will also return the first element, but you can ignore the return value if you do not want to use it. See http://www.w3schools.com/jsref/jsref_shift.asp for more information.

0
source share

All Articles