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

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

如何計算 JavaScript 數(shù)組中的項目,但僅當(dāng)項目彼此相鄰時相同?

如何計算 JavaScript 數(shù)組中的項目,但僅當(dāng)項目彼此相鄰時相同?

慕田峪4524236 2023-09-21 16:54:31
我有一個像這樣的數(shù)組{John, John, John, Maria, Peter, Peter, Maria, Anna, Anna, Maria, Maria, Peter}我需要得到像這樣的結(jié)果1 -> 32 -> 13 -> 24 -> 15 -> 26 -> 27 -> 1
查看完整描述

1 回答

?
慕尼黑8549860

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

我把名字分組,然后數(shù)數(shù)。


const array = ['John', 'John', 'John', 'Maria', 'Peter', 'Peter', 'Maria', 'Anna', 'Anna', 'Maria', 'Maria', 'Peter'];


let final = [];

const count = array.forEach(item => {

                   //check whether the last item in the array has the same name

                   if ( final[final.length - 1] && final[final.length-1][0] === item ) {

                        final[final.length -1].push(item)

                   } else {

                        //if different name then create a new grouping

                        final[final.length] = [item]

                   }

})

console.log(final.map(item => item.length)) //returns the size of each group

console.log('final array', final)


查看完整回答
反對 回復(fù) 2023-09-21
  • 1 回答
  • 0 關(guān)注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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