So the goal is to fragment an array to subarrays on the stumble of a certain element Example below for array.split("stop here")
["haii", "keep", "these in the same array but", "stop here", "then continue", "until you reach", "another", "stop here", "and finally", "stop here", "stop here"]
that to
[
["haii", "keep", "these in the same array but"], ["then continue", "until you reach", "another"], ["and finally"]
]
What I tried till now is not working very well:
Array.prototype.split = function (element) {
const arrays = [];
// const length = this.length;
let arrayWorkingOn = this;
for(let i=0; i<arrayWorkingOn.length; i++) {
if(this[i] === element) {
const left = arrayWorkingOn.slice(0, i);
const right = arrayWorkingOn.slice(i, arrayWorkingOn.length);
arrayWorkingOn = right;
arrays.push(left);
console.log(right);
}
}
arrays.push(arrayWorkingOn); //which is the last 'right'
return arrays;
}
Thanks in advance for your time and effort!