一、改变原数组的方法
++--
1.push()尾+
2. pop() 尾-
3.unshift()头加
4.shift() 头-
5.splice() 截取数组
6.reverse() 翻转数组
7.sort() 排序
语法一: 数组名.sort() 会排序 会按照位排序
语法二: 数组名.sort(function (a,b) {return a-b}) 会正序排列
语法三: 数组名.sort(function (a,b) {return b-a}) 会倒序排列
二、不改变原数组的方法
(concat,join,slice,map ,filter ,reduce)
1.concat()合并数组
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2);
console.log(array3);
// Expected output: Array ["a", "b", "c", "d", "e", "f"]
2.join() 数组转字符串
const elements = ['Fire', 'Air', 'Water'];
console.log(elements.join());
// Expected output: "Fire,Air,Water"
console.log(elements.join(''));
// Expected output: "FireAirWater"
console.log(elements.join('-'));
// Expected output: "Fire-Air-Water"
3.slice()截取数组的一部分数据 ----包前不包后
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// Expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// Expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// Expected output: Array ["bison", "camel", "duck", "elephant"]
4.indexOf 从左检查数组中有没有这个数值
如果有就返回该数据第一次出现的索引
如果没有返回 -1
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// Expected output: 1
// Start from index 2
console.log(beasts.indexOf('bison', 2));
// Expected output: 4
console.log(beasts.indexOf('giraffe'));
// Expected output: -1
5.lastIndexOf
返回数组中给定元素最后一次出现的索引,如果不存在则返回 -1
const animals = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
console.log(animals.lastIndexOf('Dodo'));
// Expected output: 3
console.log(animals.lastIndexOf('Tiger'));
// Expected output: 1
三、ES6新增的数组方法
1. forEach() 用来循环遍历的 for 无返回值
2.map 映射数组的 有返回值
3.filter 过滤数组 有返回值
返回值: 如果有就是过滤(筛选)出来的数据 保存在一个数组中;如果没有返回一个空数组
4.reduce() 求和
5.find()用来获取数组中满足条件的第一个数据
findIndex() 用来获取数组中满足条件的第一个数据下标
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// Expected output: 3
6.every 判断数组是不是满足所有条件 返回值true false
some 数组中有没有满足条件的 返回值true false
暂且无更新后续再去更新刷新一下