Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Javascript: take every nth Element of Array

var log = function(val){document.body.innerHTML+='<div></pre>'+val+'</pre></div>'} 

var oldArr = [0,1,2,3,4,5,6,7,8,9,10]
var arr = [];

var maxVal = 5;

var delta = Math.floor( oldArr.length / maxVal );

// avoid filter because you don't want
// to loop over 10000 elements !
// just access them directly with a for loop !
//                                 |
//                                 V
for (i = 0; i < oldArr.length; i=i+delta) {
  arr.push(oldArr[i]);
}


log('delta : ' + delta + ' length = ' + oldArr.length) ;
log(arr);
 Run code snippet
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript template string condtioning 
Javascript :: how to send the captured image from js to python backedn flask 
Javascript :: get page scrolling amount js 
Javascript :: set display size phaser 
Javascript :: state dependent on prev state in react js 
Javascript :: Cntrlsss:$.Control-Ai 
Javascript :: phaser place on rectangle shift 
Javascript :: phaser wrap in rectangle 
Javascript :: phaser mixed animation 
Javascript :: phaser tween timescale 
Javascript :: unicons add all icons 
Javascript :: remove text and keep div inside a div jquery 
Javascript :: Datatable js Search Server side after time or word length 
Javascript :: javascript 2021 
Javascript :: add filter category to react native flatslit 
Javascript :: get elements by class name wildcard 
Javascript :: event.target javascript 
Javascript :: javascript static methods 
Javascript :: javascript trunc 
Javascript :: npm read email 
Javascript :: javascript array map 
Javascript :: object set js 
Javascript :: convert a string to array 
Javascript :: get element by id angular 
Javascript :: Auto increment in firebase realtime database 
Javascript :: js code 
Javascript :: javascript error handling 
Javascript :: ajax form submit 
Javascript :: array js 
Javascript :: difference between dom and react dom 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =