如何在JavaScript中等分?jǐn)?shù)組的實(shí)現(xiàn)
最近開源了一個(gè) Vue 組件,還不夠完善,歡迎大家來一起完善它,也希望大家能給個(gè) star 支持一下,謝謝各位了。
github 地址:https://github.com/qq449245884/vue-okr-tree
在本教程中,我們來學(xué)習(xí)一下如何使用Array.splice()方法將數(shù)組等分,還會(huì)講一下,Array.splice() 和 Array.slice() 它們之間的不同之處。
1. 將數(shù)組分為兩個(gè)相等的部分我們可以分兩步將數(shù)組分成兩半:
使用length/2和Math.ceil()方法找到數(shù)組的中間索引
使用中間索引和Array.splice()方法獲得數(shù)組等分的部分
Math.ceil() 函數(shù)返回大于或等于一個(gè)給定數(shù)字的最小整數(shù)。
const list = [1, 2, 3, 4, 5, 6];const middleIndex = Math.ceil(list.length / 2);const firstHalf = list.splice(0, middleIndex); const secondHalf = list.splice(-middleIndex);console.log(firstHalf); // [1, 2, 3]console.log(secondHalf); // [4, 5, 6]console.log(list); // []
Array.splice() 方法通過刪除,替換或添加元素來更改數(shù)組的內(nèi)容。 而 Array.slice() 方法會(huì)先對數(shù)組一份拷貝,在操作。
list.splice(0, middleIndex) 從數(shù)組的0索引處刪除前3個(gè)元素,并將其返回。 splice(-middleIndex)從數(shù)組中刪除最后3個(gè)元素并返回它。在這兩個(gè)操作結(jié)束時(shí),由于我們已經(jīng)從數(shù)組中刪除了所有元素,所以原始數(shù)組是空的。
另請注意,在上述情況下,元素?cái)?shù)為偶數(shù),如果元素?cái)?shù)為奇數(shù),則前一半將有一個(gè)額外的元素。
const list = [1, 2, 3, 4, 5];const middleIndex = Math.ceil(list.length / 2);list.splice(0, middleIndex); // returns [1, 2, 3]list.splice(-middleIndex); // returns [4, 5]2.Array.slice 和 Array.splice
有時(shí)我們并不希望改變原始數(shù)組,這個(gè)可以配合 Array.slice() 來解決這個(gè)問題:
const list = [1, 2, 3, 4, 5, 6];const middleIndex = Math.ceil(list.length / 2);const firstHalf = list.slice().splice(0, middleIndex); const secondHalf = list.slice().splice(-middleIndex);console.log(firstHalf); // [1, 2, 3]console.log(secondHalf); // [4, 5, 6]console.log(list); // [1, 2, 3, 4, 5, 6];
我們看到原始數(shù)組保持不變,因?yàn)樵谑褂肁rray.slice()刪除元素之前,我們使用Array.slice()復(fù)制了原始數(shù)組。
3.將數(shù)組分成三等分const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];const threePartIndex = Math.ceil(list.length / 3);const thirdPart = list.splice(-threePartIndex);const secondPart = list.splice(-threePartIndex);const firstPart = list; console.log(firstPart); // [1, 2, 3]console.log(secondPart); // [4, 5, 6]console.log(thirdPart); // [7, 8, 9]
簡單解釋一下上面做了啥:
首先使用st.splice(-threePartIndex)提取了ThirdPart,它刪除了最后3個(gè)元素[7、8、9],此時(shí)list僅包含前6個(gè)元素[1、2、3、4、5、6] 。 接著,使用list.splice(-threePartIndex)提取了第二部分,它從剩余l(xiāng)ist = [1、2、3、4、5、6](即[4、5、6])中刪除了最后3個(gè)元素,list僅包含前三個(gè)元素[1、2、3],即firstPart。4. Array.splice() 更多用法現(xiàn)在,我們來看一看 Array.splice() 更多用法,這里因?yàn)槲也幌敫淖冊瓟?shù)組,所以使用了 Array.slice(),如果智米們想改變原數(shù)組可以進(jìn)行刪除它。
const list = [1, 2, 3, 4, 5, 6, 7, 8, 9];
獲取數(shù)組的第一個(gè)元素
list.slice().splice(0, 1) // [1]
獲取數(shù)組的前5個(gè)元素
list.slice().splice(0, 5) // [1, 2, 3, 4, 5]
獲取數(shù)組前5個(gè)元素之后的所有元素
list.slice().splice(5) // 6, 7, 8, 9]
獲取數(shù)組的最后一個(gè)元素
list.slice().splice(-1) // [9]
獲取數(shù)組的最后三個(gè)元素
list.slice().splice(-3) // [7, 8, 9]
到此這篇關(guān)于如何在JavaScript中等分?jǐn)?shù)組的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)JavaScript 等分?jǐn)?shù)組內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. Python 操作 MySQL數(shù)據(jù)庫3. Python數(shù)據(jù)相關(guān)系數(shù)矩陣和熱力圖輕松實(shí)現(xiàn)教程4. 開發(fā)效率翻倍的Web API使用技巧5. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 什么是Python變量作用域9. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式10. python 如何在 Matplotlib 中繪制垂直線
