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

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

JS 將數(shù)組拆分為 X 塊,然后是 Y 塊,依此類推

JS 將數(shù)組拆分為 X 塊,然后是 Y 塊,依此類推

回首憶惘然 2021-11-18 16:16:47
我究竟如何將數(shù)組拆分為 6 個塊,然后是 3 個塊,然后是 6 個塊,然后是 3 個塊等等?所以說我有這個數(shù)據(jù)集:const datasaet = [  { text: 'hi1' },  { text: 'hi2' },  { text: 'hi3' },  { text: 'hi4' },  { text: 'hi5' },  { text: 'hi6' },  { text: 'hi7' },  { text: 'hi8' },  { text: 'hi9' },  { text: 'hi10' },  { text: 'hi11' },  { text: 'hi12' },  { text: 'hi13' },  { text: 'hi14' },  { text: 'hi15' },  { text: 'hi16' },]我需要將它拆分成這樣的數(shù)組:const expected = [  [    { text: 'hi1' },    { text: 'hi2' },    { text: 'hi3' },    { text: 'hi4' },    { text: 'hi5' },    { text: 'hi6' },  ],  [    { text: 'hi7' },    { text: 'hi8' },    { text: 'hi9' },  ],  [    { text: 'hi10' },    { text: 'hi11' },    { text: 'hi12' },    { text: 'hi13' },    { text: 'hi14' },    { text: 'hi15' },    { text: 'hi16' },  ]]本質(zhì)上,我正在做的是將數(shù)組拆分為 6 塊(如果是事件)和 3 塊(如果是奇數(shù))。但是我不知道如何去做。我目前的嘗試看起來像這樣,我可以完美地將它分成 6 塊,但是我該如何做接下來的 3 塊,然后是接下來的 6 塊,依此類推:const grouped = datasaet.reduce(  (initital: any[], current, index, items) => {    const isFirstOfSix = index % 6 === 0    if (isFirstOfSix) {      const nextSix = items.slice(index, index + 6)      initital.push(nextSix)    }    return initital  },  []) 
查看完整描述

2 回答

?
阿晨1998

TA貢獻2037條經(jīng)驗 獲得超6個贊

您可能會考慮創(chuàng)建數(shù)組的副本(以避免改變原始數(shù)組),然后拼接項目直到它為空,檢查并切換指示是否在當前迭代中刪除 6 或 3 個項目的布爾值:


const datasaet = [

  { text: 'hi1' },

  { text: 'hi2' },

  { text: 'hi3' },

  { text: 'hi4' },

  { text: 'hi5' },

  { text: 'hi6' },

  { text: 'hi7' },

  { text: 'hi8' },

  { text: 'hi9' },

  { text: 'hi10' },

  { text: 'hi11' },

  { text: 'hi12' },

  { text: 'hi13' },

  { text: 'hi14' },

  { text: 'hi15' },

  { text: 'hi16' },

]


const tempArr = datasaet.slice();

const output = [];

let removeSix = true;

while (tempArr.length) {

  output.push(tempArr.splice(0, removeSix ? 6 : 3));

  removeSix = !removeSix;

}

console.log(output);


查看完整回答
反對 回復 2021-11-18
?
慕的地8271018

TA貢獻1796條經(jīng)驗 獲得超4個贊

您可以創(chuàng)建一個接受數(shù)組和塊大小數(shù)組的函數(shù)。該函數(shù)迭代數(shù)組,在塊大小之間循環(huán),并使用 slice 從原始數(shù)組中獲取當前塊大?。?/p>


const chunks = (arr, chunkSize) => {

  const result = [];

  

  let current = -1;


  for (let i = 0; i < arr.length; i += chunkSize[current]) {

    current = (current + 1) % chunkSize.length;

    

    result.push(arr.slice(i, i + chunkSize[current]));

  }


  return result;

}


const dataset = [{"text":"hi1"},{"text":"hi2"},{"text":"hi3"},{"text":"hi4"},{"text":"hi5"},{"text":"hi6"},{"text":"hi7"},{"text":"hi8"},{"text":"hi9"},{"text":"hi10"},{"text":"hi11"},{"text":"hi12"},{"text":"hi13"},{"text":"hi14"},{"text":"hi15"},{"text":"hi16"}];


const result = chunks(dataset, [6, 3]);


console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反對 回復 2021-11-18
  • 2 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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