samedi 27 juin 2015

How to get batch of array value from an array by incremental or decrements values

I have an array, It contains bunch of values in it. I would like to slice the array by increasing value of the count number or decreasing value of the count.

the count is updated by next and prev buttons. when user click on next the count increase and the user click on prev button the count decrease. using this wan to slice array values by my batch numbers.

here is my try:

var arr = [1,2,3,4,5,6,7,8,9];
var batch = 2;
var num = 2;
var total = arr.length;
var group = arr.slice(0, (batch % total));


var add = function (amount) {
     num = (num + total - 1 + amount) % total + (2)
     console.log(arr.slice((num-batch), (num % total)))
}

$('a').click(function (e) {
    var num = e.target.className == 'prev' ? -1 : 1;
    add(num);
})

console.log(group)

Live Demo

Aucun commentaire:

Enregistrer un commentaire