divide big array into multiple arrays javascript code example

Example 1: js split array into smaller arrays

Array.prototype.chunk = function(n) {
  if (!this.length) {
    return [];
  }
  return [this.slice(0, n)].concat(this.slice(n).chunk(n));
};
console.log([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15].chunk(5));

Example 2: return uncommon from two arrays js

let array = [1,2,3,4,5,6,78,9];

function except(array,excluded){
let newArr,temp,temp1;

      check1=array.filter(function(value) 
                {
                  return excluded.indexOf(value) == -1; 

                });

      check2=excluded.filter(function(value) 
                {
                  return array.indexOf(value) == -1; 

                });

    output=check1.concat(check2);


    return output;

  }


except(array,[1,2])

//so the output would be => [ 3, 4, 5, 6, 78, 9 ]

Tags:

Misc Example