Is there a method to split an array into arrays that dont contain null values using javascript (without developing a function)...
Here is an example of what I want have :
input :
var a = [1, 2, 3, null, 2, null,null, 4]
output :
[[1, 2, 3], [2], [4]]
Thank you
-
Have you tried to loop over array and create sub arrays?Tushar– Tushar2015年12月29日 15:04:50 +00:00Commented Dec 29, 2015 at 15:04
-
I know I can, but I'm asking if there is a ready method for use ... without developing a functionMohamed Taboubi– Mohamed Taboubi2015年12月29日 15:05:49 +00:00Commented Dec 29, 2015 at 15:05
-
Looping is done in about 3 lines and there's no reason there would be a generic solution to this very specific problem.Denys Séguret– Denys Séguret2015年12月29日 15:06:59 +00:00Commented Dec 29, 2015 at 15:06
-
Ok, that's fine so ... (the split function for strings is awesome -- I hoped there is one for arrays) ... Thank youMohamed Taboubi– Mohamed Taboubi2015年12月29日 15:08:52 +00:00Commented Dec 29, 2015 at 15:08
-
1A loop is the best you can doCoderPi– CoderPi2015年12月29日 15:27:06 +00:00Commented Dec 29, 2015 at 15:27
4 Answers 4
To the question
"Is there a ready to use function to build my array",
the answer is
"No, because your need is too specific".
But it's not so hard to do it yourself. Here's a solution (I took a less trivial input with null at ends and consecutive null to be more demonstrative):
var a = [null, 1, 2, 3, null, 2, null, null, 4, null];
var b = []; // result
for (var arr, i=0; i<a.length; i++) {
if (a[i]===null) {
arr=null;
} else {
if (!arr) b.push(arr=[]);
arr.push(a[i]);
}
}
document.body.textContent = JSON.stringify(b); // just print the result
As you can see, there's nothing pretty or magical, just a tedious iteration.
4 Comments
slice would be faster I thinkpush is already fast (especially on homogeneous arrays) so I doubt it really matters (but I agree it could be faster).You can use reduce:
[1, 2, 3, null, 2, null, 4].reduce(function(arr, val) {
if(val === null) arr.push([]);
else arr[arr.length-1].push(val);
return arr;
}, [[]]);
6 Comments
"_".split("_") is ["",""]. Similarly, my code returns [ [],[] ] for [null]."123_2___4".split("_") is ["123","2","","","4"]. Similarly, my code returns [[1,2,3],[2],[],[],[4]] for [1,2,3,null,2,null,null,null,4].There is no function in the library which does what you expect. But if you approach it simply, you can use the next 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);
// use your result, it gives [[1, 2, 3], [2], [4]]
i have added some functionalities to prevent problems with double null, starting null and ending null. The next step is just wrapping the above snippet in a function, using a as argument and result as return value. This even handles a = [] without problems too.
Comments
I needed this function, and using .push() isn't going to be fast enough. Here's one using .slice(), as coderPi pointed out.
function split(arr, separator = null) {
const chunks = [];
let i = 0, j = 0;
while (j++ < arr.length) {
let nextIndex = arr.indexOf(separator, i);
if (nextIndex === -1) {
chunks.push(arr.slice(i, arr.length));
return chunks;
} else if (nextIndex !== i) {
chunks.push(arr.slice(i, nextIndex));
}
i = nextIndex + 1;
}
return chunks;
}
// example:
const myChucks = split([1, 2, 3, null, 2, null, null, 4]);
// returns: [[1,2,3],[2],[4]]
document.write(JSON.stringify(myChucks))