在javascript中,将数组拆分为数组到数组的数组
因此,我们的目标是在某个元素出错时将一个数组分割成子数组 下面是array.split的示例(“停止在此处”) 那个在javascript中,将数组拆分为数组到数组的数组,javascript,arrays,multidimensional-array,split,Javascript,Arrays,Multidimensional Array,Split,因此,我们的目标是在某个元素出错时将一个数组分割成子数组 下面是array.split的示例(“停止在此处”) 那个 [ ["haii", "keep", "these in the same array but"], ["then continue", "until you reach", "another"], ["and finally"] ]
[
["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;
}
Array.prototype.split=函数(元素){
常量数组=[];
//const length=this.length;
让arrayWorkingOn=this;
对于(让i=0;i首先.join()
在我的例子中使用唯一分隔符unique\u separator
然后首先使用.split(“停在这里”)
将其拆分,它返回一个包含3个字符串的数组
现在您需要在数组上.map()
并用分隔符(唯一分隔符)将其拆分,然后.filter()
将“
值过滤掉
最后,通过检查空数组的长度过滤掉它,就完成了
让arr=[
“哈伊”,
“保留”,
“它们在同一数组中,但”,
“到此为止”,
“然后继续”,
“直到你到达”,
“另一个”,
“到此为止”,
“最后”,
“到此为止”,
“到此为止”
];
Array.prototype.split=函数(){
返回此。加入(“唯一分隔符”)
.split(“到此为止”)
.map(el=>el.split(“唯一分隔符”).filter(布尔))
.filter(arr=>arr.length);
};
console.log(arr.split());
如果要循环整个数组,则根本不使用切片
,只需在数组中累积项目,当遇到元素时,只需推送该数组并创建一个新数组,如下所示:
Array.prototype.split = function (element) {
const arrays = [];
let currentArray = []; // is used to accumulate the sub arrays
for(let item of this) { // for each item of the array
if(item === element) { // if item is the element
arrays.push(currentArray); // add the current accumulated array to arrays
currentArray = []; // and start accumulating a new one
} else { // otherwise
currentArray.push(item); // add the item to the accumulated array
}
}
arrays.push(currentArray); // don't forget the last one
return arrays;
}
注意:此答案使用了拆分的正确行为,这不是问题所要求的。如果要拆分的元素是原始数组中的第一项、最后一项或相邻项,则结果数组中应该有空数组。'abcbdb'。拆分('b')
应导致['a','c','d',''']
不是['a','c','d']
。如果要忽略空数组,请查看上面的
演示:
Array.prototype.split=函数(元素){
常量数组=[];
让currentArray=[];
对于(本项目中的项目){
如果(项目===元素){
arrays.push(currentArray);
currentArray=[];
}否则{
currentArray.push(项目);
}
}
arrays.push(currentArray);
返回阵列;
}
让结果=[“haii”、“keep”、“这些在同一数组中,但是”、“停在这里”、“然后继续”、“直到到达”、“另一个”、“停在这里”、“最后”、“停在这里”、“停在这里”]。拆分(“停在这里”);
console.log(结果);
一些想法:
重写原型是一个先进的想法,可能是不必要的
查看诸如lodash之类的实用程序库,以获取此类实用程序功能
如果您想手动执行,我会尝试:
array.join(“/$/”).split(“停在这里”).map(part=>part.split(“/$/”).filter(布尔))
首先将数组转换为具有拆分方法的字符串,然后再次转换回数组
演示:
const-array=[“haii”、“keep”、“这些在同一个数组中,但是”、“停在这里”、“然后继续”、“直到到达”、“另一个”、“停在这里”、“最后”、“停在这里”、“停在这里”]
console.log(array.join(“/$/”).split(“到此为止”).map(part=>part.split(“/$/”).filter(布尔))
这个答案的灵感来自ibrahim mahrir的答案
Array.prototype.split = function (element) {
const arrays = [];
const length = this.length;
let accumulatedArray = [];
for(let i=0; i<length; i++) {
if( this[i] === element ) {
if( accumulatedArray.length > 0 ) arrays.push(accumulatedArray);
accumulatedArray = [];
} else {
accumulatedArray.push(this[i]);
}
}
if( accumulatedArray.length > 0 ) arrays.push(accumulatedArray);;
return arrays;
}
Array.prototype.split=函数(元素){
常量数组=[];
const length=this.length;
设累加数组=[];
对于(设i=0;i0)数组,push(累加数组);
累计阵列=[];
}否则{
累计阵列推力(本[i]);
}
}
如果(累计数组.length>0)数组.push(累计数组);;
返回阵列;
}
.join(“/”
假设字符串中没有/
。这将在[“name/last name”,“stop here”,“age”]
这样的数组中失败。这不会解决问题。任何标记都可能是字符串的一部分。您的答案最简单,但我继续更新它以处理所有情况(我测试过)在我的回答中。如果我遇到了一些性能问题,请纠正我。(可能通过对最终数组进行另一次遍历来删除空数组,而不是在同一次遍历中检查每个迭代)是的,你的答案非常适合你的用例。我忘了推上一个累积数组,谢谢你指出这一点。我会保持我的原样,因为这是拆分的正确行为,可能会帮助未来的用户,你应该接受你自己的答案,因为它更适合你的问题。伙计们,谢谢你们r答案我稍后会查看它们,因为现在我得到了一个满意的答案,我将继续使用它。感谢所有的支持!
Array.prototype.split = function (element) {
const arrays = [];
const length = this.length;
let accumulatedArray = [];
for(let i=0; i<length; i++) {
if( this[i] === element ) {
if( accumulatedArray.length > 0 ) arrays.push(accumulatedArray);
accumulatedArray = [];
} else {
accumulatedArray.push(this[i]);
}
}
if( accumulatedArray.length > 0 ) arrays.push(accumulatedArray);;
return arrays;
}