There is no function in the library that does what you expect. But if you just approach it, you can use the following approach
var a = [1, 2, 3, null, 2, null, 4] var result = [], temp = []; for(var i = 0; i < a.length; i++) { if (a[i] === null) { if (temp.length == 0) continue; result.push(temp); temp = []; } else { temp.push(a[i]); } } if (temp.length != 0) result.push(temp);
I added some functions to prevent problems with double null , starting with null and ending with null . The next step is to simply wrap the above snippet in a function, using a as an argument and result as the return value. It even handles a = [] without any problems.
source share