array.split("stop here") 数组到 javascript 中的数组数组

所以我们的目标是在遇到某个元素时将一个数组分割成子数组下面的例子是 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;

}

预先感谢您的时间和精力!


Qyouu
浏览 101回答 3
3回答

慕仙森

如果您要循环整个数组,那么根本不要使用slice,只需在进入数组时累积项目,当您遇到元素时,只需推送该数组并创建一个新数组,如下所示:Array.prototype.split = function (element) {&nbsp; const arrays = [];&nbsp; let currentArray = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// is used to accumulate the sub arrays&nbsp; for(let item of this) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // for each item of the array&nbsp; &nbsp; if(item === element) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// if item is the element&nbsp; &nbsp; &nbsp; arrays.push(currentArray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// add the current accumulated array to arrays&nbsp; &nbsp; &nbsp; currentArray = [];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// and start accumulating a new one&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// otherwise&nbsp; &nbsp; &nbsp; currentArray.push(item);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// add the item to the accumulated array&nbsp; &nbsp; }&nbsp; }&nbsp; arrays.push(currentArray);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// don't forget the last one&nbsp; return arrays;}注意:此答案使用的正确行为split不是问题所要求的。如果要拆分的元素是原始数组中的第一项、最后一项或相邻项,则结果数组中应该有空数组。'abcbdb'.split('b')应该导致['a', 'c', 'd', '']not ['a', 'c', 'd']。如果您想忽略空数组,请查看上面Jhon 接受的答案。Array.prototype.split = function (element) {&nbsp; const arrays = [];&nbsp; let currentArray = [];&nbsp; for(let item of this) {&nbsp; &nbsp; if(item === element) {&nbsp; &nbsp; &nbsp; arrays.push(currentArray);&nbsp; &nbsp; &nbsp; currentArray = [];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; currentArray.push(item);&nbsp; &nbsp; }&nbsp; }&nbsp; arrays.push(currentArray);&nbsp; return arrays;}let result = ["haii", "keep", "these in the same array but", "stop here", "then continue", "until you reach", "another", "stop here", "and finally", "stop here", "stop here"].split("stop here");console.log(result);

拉风的咖菲猫

这个答案的灵感来自易卜拉欣马里尔的答案。Array.prototype.split = function (element) {&nbsp; const arrays = [];&nbsp; const length = this.length;&nbsp; let accumulatedArray = [];&nbsp; for(let i=0; i<length; i++) {&nbsp; &nbsp; if( this[i] === element ) {&nbsp; &nbsp; &nbsp; if( accumulatedArray.length > 0 ) arrays.push(accumulatedArray);&nbsp; &nbsp; &nbsp; accumulatedArray = [];&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; accumulatedArray.push(this[i]);&nbsp; &nbsp; }&nbsp; }&nbsp; if( accumulatedArray.length > 0 ) arrays.push(accumulatedArray);;&nbsp; return arrays;}

呼唤远方

在我的例子中,首先.join()你的数组有一个独特的分隔符UNIQUE_SEPERATOR然后你首先将它拆分.split("stop here"),返回一个包含 3 个字符串的数组。现在您需要.map()遍历数组并用分隔符 (UNIQUE_SEPERATOR) 将其拆分并.filter()输出""值。最后,您通过检查其长度来过滤掉空数组,然后就完成了。let arr = [&nbsp; "haii",&nbsp; "keep",&nbsp; "these in the same array but",&nbsp; "stop here",&nbsp; "then continue",&nbsp; "until you reach",&nbsp; "another",&nbsp; "stop here",&nbsp; "and finally",&nbsp; "stop here",&nbsp; "stop here"];Array.prototype.split = function() {&nbsp; return this.join("UNIQUE_SEPERATOR")&nbsp; &nbsp; .split("stop here")&nbsp; &nbsp; .map(el => el.split("UNIQUE_SEPERATOR").filter(Boolean))&nbsp; &nbsp; .filter(arr => arr.length);};console.log(arr.split());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript