美文网首页
javascript复习数组原型方法

javascript复习数组原型方法

作者: 200813 | 来源:发表于2017-02-23 12:10 被阅读0次

    <h2>forEach</h2>
    这是JavaScript原生数组方法中最简单的方法。不用怀疑,IE7和IE8不支持此方法。
    forEach方法需要一个回调函数,数组内的每个元素都会调用一次此方法,此方法需要三个参数如下:
    value 当前操作的数组元素
    当前操作元素的数组索引
    array 当前数组的引用
    fromCharCode() 可接受一个指定的 Unicode 值,然后返回一个字符串。
    charCodeAt() 方法可返回指定位置的字符的 Unicode 编码
    此外,可以传递可选的第二个参数,作为每个调用函数的上下文(this)。

       ['h','e','l','l','o'].forEach(function (value, index, array) {
        this.push(String.fromCharCode(value.charCodeAt() + index + 2))
    }, out = [])
    
    out.join('')//jhpqu
    

    <h2>some和every</h2>
    some将会给数组里的每一个元素执行一遍回调函数,直到有一个回调函数返回true位置。如果找到目标元素,some立即返回true,否则some返回false。回调函数只对已经指定值的数组索引执行;它不会对已删除的或未指定值的元素执行。

    max = -Infinity
    satisfied = [10, 12, 10, 8, 5, 23].some(function (value, index, array) {
        if (value > max) max = value
        return value < 10
    })
    
    console.log(max)
    // <- 12
    
    satisfied
    // <- true
    

    <h2>join和concat的区别</h2>
    .join方法经常和.concat混淆。.join(分隔符)方法创建一个字符串,会将数组里面每个元素用分隔符连接。如果没有提供分隔符,默认的分隔符为“,”。.concat方法创建一个新数组,其是对原数组的浅拷贝(注意是浅拷贝哦)。

    .concat 的标志用法:array.concat(val, val2, val3, valn)
    .concat 返回一个新数组
    array.concat()没有参数的情况下,会返回原数组的浅拷贝 浅拷贝意味着新数组和原数组保持相同的对象引用,这通常是好事
    <h2>栈和队列,pop,push,shift,unshift</h2>

    function Stack () {
        this._stack = []
    }
    
    Stack.prototype.next = function () {
        return this._stack.pop()
    }
    
    Stack.prototype.add = function () {
        return this._stack.push.apply(this._stack, arguments)
    }
    
    stack = new Stack()
    stack.add(1,2,3)
    
    stack.next()
    // <- 3
    

    <h2>排序sort</h2>像大部分排序函数一样,Array.prototype.sort(fn(a,b))需要一个包含两个测试参数的回调函数,并且要产生一下三种返回值之一

    [9,80,3,10,5,6].sort()
    // <- [10, 3, 5, 6, 80, 9]
    
    [9,80,3,10,5,6].sort(function (a, b) {
        return a - b
    })
    // <- [3, 5, 6, 9, 10, 80]
    

    <h2>计算reduce和reduceRight</h2>
    [x1, x2, x3, x4].reduce(f) = f(f(f(x1, x2), x3), x4)
    .reduce从左到右而.reduceRight从右到左循环遍历数组,每次调用接收目前为止的部分结果和当前遍历的值。

    两种方法都有如下典型用法:.reduce(callback(previousValue, currentValue, index, array), initialValue)。

    previousValue是最后被调用的回调函数的返回值,initialValue是开始时previousValue被初始化的值。currentValue 是当前被遍历的元素值,index是当前元素在数组中的索引值。array是对调用.reduce数组的简单引用。

    Array.prototype.sum = function () {
        return this.reduce(function (partial, value) {
            return partial + value
        }, 0)
    };
    
    [3,4,5,6,10].sum()
    // <- 28
    

    当数组的值为对象时

     function concat(input) {
        return input.reduce(function (partial, value) {
                if(!partial){
                    partial+=',';//用,链接
                }
            return partial + value
        }, '')
    };
    
    concat([
          {Tasha:hfjashf}
    ])
    

    <h2>slice用法</h2>
    slice() 方法可从已有的数组中返回选定的元素。
    arrayObject.slice(start,end)
    Array.prototype.slice能被用来将类数组对象转换为真正的数组。
    Array.prototype.slice.call({ 0: 'a', 1: 'b', length: 2 })
    // <- ['a', 'b']
    <h2>splice</h2>
    它允许你删除元素,插入新元素,或在同一位置同时进行上述操作,而只使用一个函数调用。注意和.concat和.slice不同的是.splice函数修改原数组。

    相关文章

      网友评论

          本文标题:javascript复习数组原型方法

          本文链接:https://www.haomeiwen.com/subject/xyavwttx.html