I would like such a function, I can’t write myself yet, I can’t think how to divide an array by positions.
It should work like this arraySplit(array, el => return el ? true : false);
And from the array of elements

 [item, item, item, NOPE, item, item, NOPE, item, item] 

Do

 [[item,item,item], [item,item,item], [item, item]] 

DreamChild Solution

 export function arraySplit(el, separator) { const items = Array.from(document.querySelectorAll(el)); let tmp = []; if (!items) { return tmp; } let res = [tmp]; for (let item of items) { if (!separator(item)) { tmp.push(item); } else { tmp = []; res.push(tmp); } } return res; } 
  • this is called grouping, and you can use the reduce method for grouping - Grundy

1 answer 1

Try this:

 function split(arr, delimiter) { var tmp = []; if(!arr) return tmp; var res = [tmp]; for(var i = 0; i < arr.length; i++) { if(arr[i] !== delimiter) tmp.push(arr[i]); else { tmp = []; res.push(tmp); } } return res; } 
  • It works, but I do not quite understand how. I rewrote the es5 syntax. - DimenSi
  • @DimenSi this is ES5. I did not use the features of ES6, because not all browsers support it yet. If something is not clear, ask. There is no special JS magic here - DreamChild
  • var is not exactly es5. So that's not the point. Explain why you are returning tmp, with an empty array, and as I understand it, if an element falls under the filter, we create a null array and push it into res? - DimenSi
  • @DimenSi you are confusing something. Declaring variables with var in JS has been around for centuries, from the very first versions. Let's say let appeared only with ES6, but let is not used here. - DreamChild
  • one
    Goodness hemorrhaging. I thought es2015 is es5 - DimenSi