第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

array.split("stop here") 數(shù)組到 javascript 中的數(shù)組數(shù)組

array.split("stop here") 數(shù)組到 javascript 中的數(shù)組數(shù)組

Qyouu 2023-01-06 11:09:25
所以我們的目標(biāo)是在遇到某個元素時將一個數(shù)組分割成子數(shù)組下面的例子是 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;}預(yù)先感謝您的時間和精力!
查看完整描述

3 回答

?
慕仙森

TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊

如果您要循環(huán)整個數(shù)組,那么根本不要使用slice,只需在進(jìn)入數(shù)組時累積項目,當(dāng)您遇到元素時,只需推送該數(shù)組并創(chuàng)建一個新數(shù)組,如下所示:


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;

}

注意:此答案使用的正確行為split不是問題所要求的。如果要拆分的元素是原始數(shù)組中的第一項、最后一項或相鄰項,則結(jié)果數(shù)組中應(yīng)該有空數(shù)組。'abcbdb'.split('b')應(yīng)該導(dǎo)致['a', 'c', 'd', '']not ['a', 'c', 'd']。如果您想忽略空數(shù)組,請查看上面Jhon 接受的答案。

Array.prototype.split = function (element) {

  const arrays = [];


  let currentArray = [];

  for(let item of this) {

    if(item === element) {

      arrays.push(currentArray);

      currentArray = [];

    } else {

      currentArray.push(item);

    }

  }

  arrays.push(currentArray);


  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);


查看完整回答
反對 回復(fù) 2023-01-06
?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗 獲得超2個贊

這個答案的靈感來自易卜拉欣馬里爾的答案。


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;

}


查看完整回答
反對 回復(fù) 2023-01-06
?
呼喚遠(yuǎn)方

TA貢獻(xiàn)1856條經(jīng)驗 獲得超11個贊

在我的例子中,首先.join()你的數(shù)組有一個獨特的分隔符UNIQUE_SEPERATOR


然后你首先將它拆分.split("stop here"),返回一個包含 3 個字符串的數(shù)組。


現(xiàn)在您需要.map()遍歷數(shù)組并用分隔符 (UNIQUE_SEPERATOR) 將其拆分并.filter()輸出""值。


最后,您通過檢查其長度來過濾掉空數(shù)組,然后就完成了。


let arr = [

  "haii",

  "keep",

  "these in the same array but",

  "stop here",

  "then continue",

  "until you reach",

  "another",

  "stop here",

  "and finally",

  "stop here",

  "stop here"

];


Array.prototype.split = function() {

  return this.join("UNIQUE_SEPERATOR")

    .split("stop here")

    .map(el => el.split("UNIQUE_SEPERATOR").filter(Boolean))

    .filter(arr => arr.length);

};


console.log(arr.split());


查看完整回答
反對 回復(fù) 2023-01-06
  • 3 回答
  • 0 關(guān)注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號