所以我们的目标是在遇到某个元素时将一个数组分割成子数组下面的例子是 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"]
那个
[
["haii", "keep", "these in the same array but"], ["then continue", "until you reach", "another"], ["and finally"]
]
到目前为止,我尝试的效果不是很好:
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;
}
预先感谢您的时间和精力!
慕仙森
拉风的咖菲猫
呼唤远方
相关分类