重写一些js数组方法(4)

shift

// 删除并返回数组的第一个元素。

Array.prototype.shift = function () {
    let firstItem = this[0]
    this.splice(0,1)
    return firstItem
}

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(fruits.shift())
console.log(fruits)

slice

Array.prototype.slice = function (start = 0, end = this.length) {
    if (!isNaN(start) && !isNaN(end)) {
        if (Number(start) < 0) {
            start = this.length + Number(start)
        } else {
            start = Number(start)
        }
        let arr = []
        for (let i = start; i < Number(end); i++) {
            arr.push(this[i])
        }
        return arr
    } else {
        throw new Error("传数字")
    }
}

console.log([1, 2, 3].slice())  //[ 1, 2, 3 ]


var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
console.log(fruits.slice(1,3)) //[ 'Orange', 'Lemon' ]

some

Array.prototype.some = function(f){
    if(f&&typeof f === "function"){
        for(let i=0;i<this.length;i++){
            if(f(this[i],i,this)){
                return true
            }
        }
        return false
    }else{
        throw new Error("传函数")
    }
}

var ages = [3, 10, 18, 20];

function checkAdult(age) {
    return age >= 18;
}

console.log(ages.some(checkAdult)) //true

sort

//小写大于大写
console.log("a" > "A")  //true
console.log("a" > "z")  //false
console.log("a" > "Z")  //true

//数字和字母比较起来都是false
console.log("a" > 100)  //false
console.log("a" > 0)  //false
console.log("a" > -111111)  //false
console.log(-111111 > "a")  //false

console.log(["a", "A", 100, 11, "Z", "z", 33].sort())
//[
//     100, 11,  33,
//     'A', 'Z', 'a',
//     'z'
//   ]
//   默认sort内处理是字母大于数字
// 使用数字排序,你必须通过一个函数作为参数来调用!!!

// 所以sort不传的话只对字母进行排序
// a-b为升序

Array.prototype.sort = function (f) {
    if (f && typeof f === "function") {
        for (let i = 0; i < this.length; i++) {
            for (let j = 0; j < this.length; j++) {
                if (f(this[j],this[i] ) > 0) {
                    let temp = this[j]
                    this[j] = this[i]
                    this[i] = temp
                }
            }
        }

    } else {
        //不细处理了,大概思路:空数组a收集英文,原数组去掉,将a排序,再类似concat 插入尾部
    }
}

var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b}); //[ 1, 5, 10, 25, 40, 100 ]
console.log(points)

unshift

Array.prototype.unshift = function(){
    let length = arguments.length

    this.length+=length
    for(let i=this.length-1;i>0;i--){
        this[i] = this[i-length]
    }
    
    for(let i=0;i<length;i++){
        this[i] = arguments[i]
    }
    return this.length
}

let arr = [1,2,3]
console.log(arr.unshift(4,5,6,7))  //7
console.log(arr)  
// [
//     4, 5, 6, 7,
//     1, 2, 3
// ] 

splice(比较复杂)

//JavaScript通过设置数组的length属性来截断数组是惟一一种缩短数组长度的方法.

Array.prototype.splice = function (index,howmany) {
    if(!isNaN(index)){
        for(let i=0;i<this.length;i++){
            if(Number(index)===i){
                let length =this.length-Number(index)
                if(!isNaN(howmany)){
                    length = howmany
                }else{
                    throw new Error("数字")
                }

                for(let j=Number(index);j<Number(index)+length;j++){
                    //左移
                    for(let k=Number(index)+1;k<this.length;k++){
                            this[k-1] = this[k]
                    }
                    this.length--
                }
                
                //添加尾项
                for(let l = 2;l<arguments.length;l++){
                    this[Number(index)+l-2] = arguments[l]
                    console.log(Number(index)+length+l-2)
                }
            }
        }
    }else{
        throw new Error("必传且数字")
    }
}

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,2,"oh","baby");
console.log(fruits)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容