JS数组方法总结

2021/8/14 23:06:23

本文主要是介绍JS数组方法总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

数组的构造器方法:

1.直接定义, var arr = []

2.使用Array构造器, var arr = Array[6]

3.Array.of():用于将参数依次转换为数组中的一项,然后返回新数组,与构造器Array的区别是只有一个参数数字时,Array是新建一个为该长度的数组,而Array.of()是新建一个只有该数字的数组

4.Array.from:从一个类似数组的可迭代对象中创建一个新的数组实例

接收三个参数:类似数组的对象,加工函数(新生成的数组会经过该函数的加工再返回),this作用域(表示加工函数执行时的this)(后两个参数可选)

console.log(Array.from('foo'));
// expected output: Array ["f", "o", "o"]

console.log(Array.from([1, 2, 3], x => x + x));
// expected output: Array [2, 4, 6]

如何判断一个变量是不是数组

1.Array.isArray()

2.a instanceof Array

3.a.constructor === Array

4.Array.prototype.isPrototypeOf(a)

5.Object.getPrototypeOf(a) === Array.prototype

6.object.prototype.toString.call(a) === [object Array]

数组中的重要方法

改变数组自身的方法

pop,push,reverse,shift,sort,splice,unshift,copyWith,fill

pop():用于删除数组中的最后一个值,并返回该元素的值

push():用于向数组末尾添加一个或多个元素,并返回该数组的长度

reverse():用于将数组进行颠倒顺序,返回的元素就是颠倒顺序后数组

shift():用于删除数组中的第一个元素,并返回该元素的值

unshift():用于向数组头部添加一个或多个元素,并返回该元素的值

sort():用于将数组中的元素进行排序后返回,默认排序顺序是在将元素转换为字符串,然后比较它们的UTF-16代码单元值序列时构建的

可以接收函数参数,接收两个比较的参数(a,b),计算后的值(例如a-b)如果小于0,则前一个元素在第二个元素之前(a,b)按照升序排序

splice():通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容

接收三个参数,第一个参数表示开始修改的位置,第二个参数表示删除的个数,第三个参数表示插入的参数,二三参数是可选的

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "May"]

fill():对指定区间的元素进行替换

接收三个参数,第一个参数表示更改的元素,第二个和第三个参数表示区间

const array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4
console.log(array1.fill(0, 2, 4));
// expected output: [1, 2, 0, 0]

// fill with 5 from position 1
console.log(array1.fill(5, 1));
// expected output: [1, 5, 5, 5]

console.log(array1.fill(6));
// expected output: [6, 6, 6, 6]

不改变自身数组的方法

concat,join,slice,toString,toLocateString,indexOf,lastIndexOf,includes

concat():连接两个数组

join():将数组中元素加入某个符号,拼接成字符串

slice():接收两个参数,起始位置和结束位置,表示截取后的数组,这两个参数可以省略不写

toString():将数组转换成字符串并以逗号拼接

indexOf():接收一个参数,表示该参数在数组中的位置,没找到则返回-1

includes():接收一个参数,表示该数组是否含有该元素,返回布尔值

数组的遍历方法

forEach:为每个数组元素执行一次提供的函数

every:测试数组中的所有元素是否通过提供的函数实现的测试,返回一个布尔值

const isBelowThreshold = (currentValue) => currentValue < 40;

const array1 = [1, 30, 39, 29, 10, 13];

console.log(array1.every(isBelowThreshold));
// expected output: true

some:测试数组中是否有元素通过提供的函数实现的测试,返回一个布尔值

filter:创建一个新数组,其中包含通过提供的函数实现的测试的所有元素

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

map:创建一个新数组,其中填充了对调用数组中的每个元素调用提供的函数的结果

const array1 = [1, 4, 9, 16];

// pass a function to map
const map1 = array1.map(x => x * 2);

console.log(map1);
// expected output: Array [2, 8, 18, 32]

reduce:创建一个新数组,调用提供的函数结果

const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;

// 1 + 2 + 3 + 4
console.log(array1.reduce(reducer));
// expected output: 10

// 5 + 1 + 2 + 3 + 4
console.log(array1.reduce(reducer, 5));
// expected output: 15

 



这篇关于JS数组方法总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程