2

i need some help manipulating/paginating a JS array object by 5:

var musicItems = [
    ["assets/audio/1.wav"],
    ["assets/audio/2.wav"],
    ["assets/audio/3.wav"],
    ["assets/audio/4.wav"],
    ["assets/audio/5.wav"],
    ["assets/audio/6.wav"],
    ["assets/audio/7.wav"],
    ["assets/audio/8.wav"],
];

I want to know in JS the number of sets (2) and which elements belong to which set (set1->1-5, set2->6-8). Appreciate any help.

EDIT I am looking to do something like Split array into chunks

But as you can see, my array is different.

3
  • possible duplicate of Splitting a JS array into N arrays Commented Mar 3, 2015 at 21:01
  • Array.prototype.map and modulo % 5 Commented Mar 3, 2015 at 21:01
  • Its not a duplicate. But i already got my answer, thank you Francodi.And albciff, you are ignorant. Commented Mar 4, 2015 at 22:15

2 Answers 2

4

This is just a simple demo on how you could achieve this with the modulo operator %. There will be more efficient and smaller solutions.

var musicItems = [
    ["assets/audio/1.wav"],
    ["assets/audio/2.wav"],
    ["assets/audio/3.wav"],
    ["assets/audio/4.wav"],
    ["assets/audio/5.wav"],
    ["assets/audio/6.wav"],
    ["assets/audio/7.wav"],
    ["assets/audio/8.wav"],
];

var sets = new Object();
var set = new Array();
var setCounter = 0;

for(var i = 0; i < musicItems.length; i++) {
    set.push(musicItems[i]);

    if((i + 1) % 5 == 0 || (i + 1) >= musicItems.length) {
        setCounter++;
        sets['set' + setCounter] = set;
        set = new Array();
    }
}

console.log(sets['set1']);
console.log(sets['set2']);

Basically what this does is to iterate through the musicItems, with modulo % 5 we check if the current item can be devided by 5 without rest, so we know a set is complete as we collected 5 items. Then we add the set to the overall sets object to use it later on as a dictionary (as wished with 'set1', 'set2' etc.) and we clear the current set, to fill it with the next n-5 items.

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

Comments

0

Bref :

  max=5,j=0;
  for(var i=0;i<musicItems.length;i=i+max){
        j++;
       sets['set' + j] = musicItems.offset(i).max(max);
  }

Explanation

Pagination concept rely on two parameters:

  1. Offset :
  2. Max :

Then :

musicItems.offset(0).max(5) ;   // 1st set
   // >  [ ["assets/audio/1.wav"],["assets/audio/2.wav"],["assets/audio/3.wav"],["assets/audio/4.wav"],["assets/audio/5.wav"]  ]
musicItems.offset(0+5).max(5) ;  // second set
   // >  [ ["assets/audio/6.wav"],["assets/audio/7.wav"],["assets/audio/8.wav"] ]

Required API :

Array.prototype.max = function(mx) {
   return this.filter(function(e,i){return i < mx;}); }
};

Array.prototype.offset=function(os){
   return this.filter(function(e,i){return i> os-1 });
 };

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.