美文网首页
js 数组方法

js 数组方法

作者: Super曲江龙Kimi | 来源:发表于2020-02-03 17:35 被阅读0次

1 Array.from

Array.from 用于将类数组转换成数组。只要有length属性

Array.from({length:3})  // [undefined, undefined, undefined]

第二个参数回调可以对数组进行map

Array.from({length:3}, val => 'kimi')  // ['kimi', 'kimi', 'kimi']

2 Array.of

Array.of用于新建数组,来弥补之前构造函数Array()创建的不足。

Array() // []
Array(3) // [, , ]   一个参数代表长度
Array(1,2,3) // [1,2,3]

Array.of() // []
Array.of(3) // [3]
Array.of(1,2,3) // [1,2,3] 

3 Array.keys()、 Array.values()、 Array.entries()

这三个api都用于遍历数组,返回的都是一个遍历器对象,用for of遍历。

const a = [1,2,3].keys(); // Iterator
const b = [1,2,3].values(); // Iterator
const c = [1,2,3].entries(); // Iterator
for (let [v, k] of c) {
    console.log(v, k)  // 0 1,   1 2,   2 3
}
console.log(a.next().value) // 0 

4 Array.map

map polyfill

Array.prototype._map = function(fn, context) {
    const results = [];
    for (let i = 0; i < this.length; i++) {
        results.push(fn.call(context, this[i], i, this));
    }
    return results;
}
const a = [1,2,3,45]
console.log(a._map(v => v*2));

5 Array.filter

filter polyfill

Array.prototype._filter = function(fn, context) {
    const results = [];
    for(let i = 0; i < this.length; i++) {
        if (fn.call(context, this[i], i, this)) results.push(this[i])
    }
    return results;
}
const a = [1,2,3,4,5,7];
console.log(a._filter(val => val > 4));

6 Array.some()、 Array.every()

polyfill

Array.prototype._every = function(fn, context) {
    for(let i = 0; i < this.length; i++) {
        if (!fn.call(context, this[i], i, this)) return false;
    }
    return true;
}
const b = ['a', 'v', 'abc'];
const c = {a:'a'}
console.log(b._every(function(val) {
    return val.indexOf(this.a) > -1
}, c))

Array.prototype._some = function(fn, context) {
    for (let i = 0; i < this.length; i++) {
        if (fn.call(context, this[i], i, this)) return true;
    }
    return false;
}
const b = ['e', 'av', 'bc'];
const c = {a:'a'}
console.log(b._some(function(val) {
    return val.indexOf(this.a) > -1
}, c))

7 Array.reduce()

reduce polyfill

Array.prototype._reduce = function(fn, initVal) {
    let res = initVal || Array.prototype.shift.call(this),
        startI = initVal ? 0 : 1;
    
    for (let i = startI; i < this.length; i++) {
        res = fn.call(this, res, this[i], i, this);
    }
    return res;
}

let a = [1,2,3,4].reduce(function(memo, val, index, arr) {
    return memo += val;
})

相关文章

  • 数组(Array)<迭代器>

    一、Js数组迭代器方法 主要介绍js数组中的forEach,every,some,filter,map迭代器方法 ...

  • js基础了解

    js数组常用遍历方法使用: js数组常用操作方法使用: 基本逻辑运算: 基本字符串操作方法:

  • js 数组

    js 中数组的长度发生改变,数组就真的发生改变,快速清空数组就让数组长度为0js 数组的方法

  • js关于数组的方法汇总

    js关于数组的方法汇总

  • 封装常用数组操作函数

    1. 数组扁平化 方法一 : 递归迭代数组 方法二 : 通过js原生 falt方法展开数组 方法三 通过正则...

  • JS数组常用方法

    @[toc] JS数组方法 数组是 js 中最常用到的数据集合,其内置的方法有很多,熟练掌握这些方法,可以有效...

  • JS如何判断一个数组是否为空、是否含有某个值

    一、js判断数组是否为空方法一: arr.length 二、js判断数组是否含有某个值方法一: arr.ind...

  • 数组进阶

    JS数组奇巧淫技 数组进阶方法~ 数组使用方法比较多。什么时候使用什么方法,用对方法,不用对很大的原因就是数组方法...

  • 数组常用方法

    数组常用方法 一、js数组常用方法: 1、join() Array.join() 方法将数组中所有元素都转换成字...

  • js 数组操作探究

    有空闲时间了, 深入的研究一下js 中的数组方法 js中的数组方法 首先是会改变原数组的方法: shift un...

网友评论

      本文标题:js 数组方法

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