数组自定义unshift和去重
arr = [1, 2, 4, 5] arrObj = Object.assign([], arr) //自定义实现数组 unshift Array.prototype.myunshift = function (...eles) { const len = this.length for (let i = 1; i <= len; i++) { arr[i] = arrObj[i - 1] } var args = Array.from(eles); arr[0] = args.pop() arrObj = Object.assign([], this) if (args.length > 0) { this.length = len + 1 this.myunshift(...args) } return len + arguments.length } Array.prototype.myunshift1 = function () { const len = arguments.length for (let i = len - 1; i >= 0; i--) { const ele = arguments[i] this.splice(0, 0, ele) } return this.length } // ret = arr.myunshift1(8, 7, 5) // console.log(arr, ret) arr1 = [1, 3, 4, 5, 7, 3, 5] //数组去重 Array.prototype.myUnique = function () { //return Array.from(new Set(this)) const retArr = [] for (let i = 0; i < this.length; i++) { if (!retArr.includes(this[i])) { retArr.push(this[i]) } } return retArr } console.log(arr1.myUnique())