改变原数组的方法
push(),pop(),shift(),unshift(),reverse(),sort(),splice(),copyWithin(),fill()
Array.prototype.push()
指定的元素(一个或多个)添加到数组的末尾,返回值为新的数组长度,会改变原数组。
const animals = ['pigs', 'goats', 'sheep'];
console.log(animals.push('cows')); //4
console.log(animals); //["pigs", "goats", "sheep", "cows"]
Array.prototype.pop()
从数组中删除最后一个元素,返回值为删除元素的值。会改变原数组。
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop()); //tomato
console.log(plants); // ['broccoli', 'cauliflower', 'cabbage', 'kale']
Array.prototype.unshift()
指定元素(一个或多个)添加到数组的开头,返回值为数组的新长度。会改变原数组。
const array1 = [1, 2, 3];
console.log(array1.unshift(4, 5)); //5
console.log(array1); // [4, 5, 1, 2, 3]
Array.prototype.shift()
从数组中删除第一个元素,返回值为删除元素的值。会改变原数组。
const array1 = [1, 2, 3];
console.log(array1.shift()); // 1
console.log(array1); //[2,3]
Array.prototype.reverse()
反转数组中的元素,返回值为反转后的数组,会改变原数组
const array1 = ['one', 'two', 'three'];
console.log(array1); //["one", "two", "three"]
const reversed = array1.reverse();
console.log(reversed); //["three", "two", "one"]
console.log('array1:', array1); //["three", "two", "one"]
Array.prototype.sort()
对数组的元素进行排序,返回为排序后的数组。默认排序是将元素转换为字符串,然后按照它们的 UTF-16 码元值升序排序。会改变原数组。
const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months); //["Dec", "Feb", "Jan", "March"]
const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1); //[1, 100000, 21, 30, 4]
const array2=[1,7,6,4,8,5]
const list2= array2.sort((a,b)=>{
return a-b;
})
console.log(list2); //[1, 4, 5, 6, 7, 8]
console.log(array2); //[1, 4, 5, 6, 7, 8]
const array3=[1,7,6,4,8,5]
const list3= array3.sort((a,b)=>{
return b-a;
})
console.log(list3); // [8, 7, 6, 5, 4, 1]
console.log(array3); // [8, 7, 6, 5, 4, 1]
Array.prototype.splice()
移除或者替换已存在的元素和/或添加新元素,返回值为删除元素的数组,没有删除返回空数组,会改变原数组。
splice(start, deleteCount, item1, item2, itemN)
start:开始改变数组的位置
deleteCount:数组中要从 start 开始删除的元素数量
item:start 开始要加入到数组中的元素
const months = ['Jan', 'March', 'April', 'June'];
const removed1 = months.splice(1, 0, 'Feb');
console.log(months); //['Jan', 'Feb', 'March', 'April', 'June']
console.log(removed1 ); //[]
const removed2=months.splice(4, 1, 'May');
console.log(months); // ['Jan', 'Feb', 'March', 'April', 'May']
console.log(removed2); // ['June']
Array.prototype.fill()
用一个固定值填充一个数组中。返回值为填充的数组,会改变原数组
fill(value, start, end)
value:填充的值,填充引用类型浅拷贝
start:开始索引
end:结束索引
const array1 = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4)); //[1,2,0,0]
console.log(array1.fill(5, 1)); //[1, 5, 5, 5]
console.log(array1.fill(6)); //[6, 6, 6, 6]
Array.prototype.copyWithin()
浅复制数组的一部分到同一数组中的另一个位置,返回复制的数组,会改变原数组
copyWithin(target, start, end)
target:替换的目标位置
start:复制的元素序列的起始位置
end:复制的元素序列的结束位置
const array1 = ['a', 'b', 'c', 'd', 'e'];
console.log(array1.copyWithin(0, 3, 4)); //["d", "b", "c", "d", "e"]
console.log(array1.copyWithin(1, 3)); //["d", "d", "e", "d", "e"]
不改变原数组的方法
Array.prototype.concat()
合并两个或多个数组,返回值为合并后的新数组,不会改变原数组。(数组的浅拷贝)
concat(value0, value1, ...,valueN) valueN数组或值,将被合并到一个新的数组中。没有值现存数组浅拷贝
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2,'g');
console.log(array1); //['a', 'b', 'c']
console.log(array3); //['a', 'b', 'c', 'd', 'e', 'f', 'g']
Array.prototype.join()
将一个数组的所有元素连接成一个字符串,返回值为拼接的字符串,用逗号或指定的分隔符字符串分隔
join(separator) separator省略,数组元素用逗号(,)分隔。如果 separator 是空字符串(""),则所有元素之间都没有任何字符。
const array1 = ['a', 'b', 'c'];
const array2 = ['d', 'e', 'f'];
const array3 = array1.concat(array2,'g');
console.log(array1); //['a', 'b', 'c']
console.log(array3); //['a', 'b', 'c', 'd', 'e', 'f', 'g']
Array.prototype.slice()
返回一个新的数组对象,这一对象是一个由 start和end决定的原数组的浅拷贝(包括 start,不包括 end),start 和 end 代表了数组元素的索引。
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2)); //["camel", "duck", "elephant"]
console.log(animals.slice(2, 4)); //["camel", "duck"]
console.log(animals.slice(1, 5)); //["bison", "camel", "duck", "elephant"]
console.log(animals.slice(-2)); //["duck", "elephant"]
console.log(animals.slice(2, -1)); //["camel", "duck"]
console.log(animals.slice()); //["ant", "bison", "camel", "duck", "elephant"]
Array.prototype.indexOf() Array.prototype.lastIndexOf(同理)
返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。
indexOf(searchElement, fromIndex)
searchElement:查找的元素
fromIndex:开始查找的下标
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison')); //1
console.log(beasts.indexOf('bison', 2)); //4
console.log(beasts.indexOf('giraffe')); //-1
//indexOf()存在问题,没办法判断+0,-0,NaN
const array = [NaN,+0];
console.log(array.indexOf(NaN));//-1
console.log(array.indexOf(-0)); //1
array.indexOf(NaN);
array.indexOf(-0);
Array.prototype.forEach()
对数组的每个元素执行一次给定的函数。没有返回值
forEach((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const array1 = ['a', 'b', 'c'];
array1.forEach((item,index,arr) => console.log(item,index,arr));
Array.prototype.map()
创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。返回一个新数组,需要return
map((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const array1 = [1, 4, 9, 16];
const map1 = array1.map((item,index,arr) => x * 2);
console.log(map1); //[2, 8, 18, 32]
Array.prototype.filter()
筛选函数实现的测试的所有元素,return返回一个新数组,如果没有返回一个空数组,需要return
filter((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter((item,index,arr) => word.length > 6);
console.log(result); //["exuberant", "destruction", "present"]
Array.prototype.every()
测试一个数组内的所有元素是否都能通过指定函数的测试。返回值为布尔值。全部符合为true,否则false,需要return
every((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every(item=>item<40)); //true
Array.prototype.some()
数组中是否至少有一个元素通过了由提供的函数实现的测试。返回值为布尔值。只要一个符合为true,否则false,需要return
some((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const array = [1, 2, 3, 4, 5];
console.log(array.some((item) => item % 2 === 0)); //true
Array.prototype.reduce()
数组中的每个元素按序执行一个提供的 reducer 函数,每一次运行 reducer 会将先前元素的计算结果作为参数传入,最后将其结果汇总为单个返回值。需要return
reduce((pre,next,index,arr)=>{},init)
pre:一开始就是初始值 当第一次有了结果以后;这个值就是第一次的结果
next:数组中的每一项
index:数组的下标
arr:原数组
init:初始值
const array1 = [1, 2, 3, 4];
const sumWithInitial = array1.reduce( //有初始值
(pre, next,index,arr) => {
console.log(pre,next,index,arr); // 0 1 1 [1, 2, 3, 4]
return pre + next; // 1 2 3 [1, 2, 3, 4]
// 3 3 6 [1, 2, 3, 4]
}, 0); // 6 4 10 [1, 2, 3, 4]
console.log(sumWithInitial); //10
const sumWithInitial1 = array1.reduce( //无初始值
(pre, next,index,arr) => {
console.log(pre,next,index,arr); // 1 2 3 [1, 2, 3, 4]
return pre + next; // 3 3 6 [1, 2, 3, 4]
// 6 4 10 [1, 2, 3, 4]
});
console.log(sumWithInitial1); //10
Array.prototype.find()
返回数组中满足提供的测试函数的第一个元素的值,需要return,没有符合的值返回undefined
find((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(item => item> 10);
console.log(found); //12
Array.prototype.findIndex()
findIndex() 方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回 -1。
findIndex((item,index,arr)=>{},this)
item:数组的当前项
index:数组的下标
arr:原数组
const array1 = [5, 12, 8, 130, 44];
console.log(array1.findIndex(item => item> 13)); //3
Array.prototype.includes()
判断一个数组是否包含一个指定的值,返回布尔值,如果包含则返回true,否则返回false。
includes(value,index)
value:查找的值
index:开始查找的下表,默认0
const array1 = [1, 2, 3];
console.log(array1.includes(2)); //true
const pets = ['cat', 'dog', 'bat']; //true
console.log(pets.includes('cat'));
console.log(pets.includes('at')); //false
Array.prototype.at()
at(num) 方法接收一个整数值并返回该索引对应的元素,允许正数和负数。负整数从数组中的最后一个元素开始倒数。返回对应的项
const array1 = [5, 12, 8, 130, 44];
console.log(array1.at(2)); //8
console.log(array1.at(-2)); //130
Array.prototype.flat()
据指定深度递归地将所有子数组元素拼接到新的数组中。返回一个新数组,
flat(depth) depth默认1,可以是Infinity
const arr1 = [0, 1, 2, [3, 4]];
console.log(arr1.flat()); //[0, 1, 2, 3, 4]
const arr2 = [0, 1, 2, [[[3, 4]]]];
console.log(arr2.flat(2)); //[0, 1, 2, [3, 4]]
console.log(arr2.flat(Infinity)); //[0, 1, 2, 3, 4]
Array.prototype.flatMap()
对数组中flatMap的每个元素应用给定的回调函数,然后将结果展开一级,返回一个新数组。等价于在调用 map()方法后再调用flatMap((item,index,arr,this)=>{})
item:数组的当前项
index:数组的下标
arr:原数组
const arr1 = [1, 2, 1];
const result = arr1.flatMap((num) => (num === 2 ? [2, 2] : 1));
console.log(result); //[1, 2, 2, 1]