給Array實現一個方法,去重後返回重複的字元
阿新 • • 發佈:2019-01-28
程式碼如下:
let arr = [1, 6, 8, 3, 7, 9, 2, 7, 2, 4, 4, 3, 3, 1, 5, 3] Array.prototype.removeDuplication = function () { let duplication = new Set() // let this = this this.map((item, index) => { let lastIndexOfItem = this.lastIndexOf(item) while (index !== lastIndexOfItem) { duplication.add(item) this.splice(lastIndexOfItem, 1) lastIndexOfItem = this.lastIndexOf(item) } }) return duplication } console.log(arr.removeDuplication()) console.log(arr)