2

I got the following array:

var liList = $(".paging li");

Right now it's filled like this: ["Previous", "Next"].
Now I want to add numbers between those two items to get something like ["Previous", "1", "2", "Next"].
How do I do this? I want something like this:

var liList = $(".paging li");
for (var i = 1; i < 10; i++){
    liList.eq(i).add("li");
} 
Grafit
  • 681
  • 11
  • 36
  • Possible duplicate of: http://stackoverflow.com/questions/586182/insert-item-into-array-at-a-specific-index, the keyword you are looking for is 'insert/insertion' – Christophe Roussy Dec 18 '14 at 16:15

1 Answers1

1

You can use the array_splice function:

$(function() {
    var pagination = ["Previous", "Next"];
    for (i = 1; i <= 10; i++ ) {
        pagination.splice(i, 0, i);
    }
    console.log(pagination);
});

Output is:

 ["Previous", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Next"]
vaso123
  • 12,347
  • 4
  • 34
  • 64