I’m new. I have an array
[1, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 5, 6]
I want to get a two-dimensional array from it
[[0, 0, 0,0],
[0, 0, 0, 0, 0, 0, 0,]]
how to do this without regular expressions
I have a solution where I use regular expressions with the split method, but need a variant without regular expressions and so that you get an array of zeros, not a string of zeros
function zeroGroups(arr){
const allZerosGroups = arr
.map(item => item !== 0 ? '-' : 0)
.join('')
.split(/[^0]/)
.filter(item => item.length !== 0)
return allZerosGroups
}
console.log(zeroGroups([1, 0, 0, 0, 0, 2, 4, 0, 0, 0, 0, 0, 0, 0, 5, 6])) // [ '0000', '0000000' ]
2
Answers
Loop through and check along the way. This is one way to do it:
Hope, this helps.