1.栈方法:push()和pop()
push()可以接受任意数量的参数,把它们添加到数组末尾,并返回数组的length值。
const numbers = [1,2,3,4];
const pushResult = numbers.push(5);
alert(pushResult); //5
pop()方法从数组末尾移除最后一项,减少数组的length值,并返回被删除的项。
const popResult = numbers.pop();
alert(popResult) //5
这两个方法可以使数组表现得像栈(后进先出LIFO)一样,最后被添加的项,最先被移除。
2.队列方法:shift()和unshift()
shift()方法与push()相反,它接受任意数量的参数,把它们添加到数据开头部分,并返回数组的length值。
const numbers = [1,2,3,4];
const shiftResult = numbers.shift(-1,0);
alert(shiftResult); //6
unshift()方法移除数组的第一项,返回被删除的项。
const unshiftResult = numbers.unshift();
alert(unshiftResult); //-1
使用shift()与push()方法,可以全JS中的数据表现的像队列(先进先出FIFO)一样,在数组末尾添加项,在数组形头移除项。
3.重排序方法:reverse()和sort()
reverse()方法会反转数组项的顺序,但是不够灵活,所以有了下面的方法。
sort()方法按升序排列数组项,sort()方法会对数组中的第一项调用toString()方法,然后对数组项进行排序,所以会出现下面的情况:
var values = [0,1,3,12,15];
values.sort()
alert(values) //0,1,12,15,3
可以看到并不是我们想要的结果,因为它是按照字符串来比较的,并不是数字,所以在很多情况下,这并不是最佳的方案,因此sort()可以接受一个比较函数来做为参数。
function compare(value1,value2) {
if (value1 < value2) {
reuturn 1
} else if (value1 > value2) {
return -1
} else {
return 0
}
}
var values = [0,1,3,12,15];
values.sort(compare)
alert(values) // 0,1,3,12,15
数值保持了正确的升序,当然也通过改变compare函数里的返回值来产生降序的结果。
4.操作方法:concat()、slice()、splice()
concat()方法基于当前数组项,创建一个新的数组,在没有给concat()传参的情况下,复制当前的数组并返回当前数组的副本。如果给concat()传一个或者多个数组,则会将这些数组中的每一项都添加到结果数组中,如果入参不是数组,也会被添加到结果数组里。
const colors = ['red', 'blue', 'green'];
const conlors2 = colors.concat();
const conlors3 = colors.concat('yellow', ['black', 'brown'], {a:1})
alert(conlors2) //red,blue,green
console.log(colors3) //["red", "blue", "green", "yellow", "black", "brown", {…}]
concat()方法不会改变原数组。 slice()方法可以基于当前数组中一项或者多项创建一个新的数组。它可以接受一个或者两个参数,当传一个参数的时候,slice()方法返回从该参数开始到数组末尾的所有项,如果传入两个参数,则返回从第一个参数开始,到第二个参数结尾的数组,但是不包括结束位置的项。
const colors = ['red', 'green', 'blue', 'yellow'];
const colors2 = colors.slice(1);
const colors3 = colors.slice(1,3);
alert(colors2); //green
alert(colors3); //green,blue
如果传入参数为负数,则用该数组的长度加上该参数来确定相应的位置。
const colors4 = colors.slice(-1);
const colors5 = colors.slice(-3,-1)
alert(colors4) // yellow
alert(colors5) // green,blue
如果结束位置小于起始位置,则返回空数组
spice()方法,它有很多种用法,主要用途是在数组的中部插入、删除、替换项,使用方式有以下3种: (1) 删除:可以删除任意数量的项,需传入两个参数,第一个参数为要删除的第一项的位置,及要删除的数量,并返回被删除的项。
const colors = ['red', 'green', 'yellow', 'blue'];
colors.splice(0,2);
alert(colors) //yellow,blue
(2)插入:可以在数组中插入任意数量的项,需传入三个参数,第一个参数是要插入项的起始位置,当只是插入数据时,第二个参数应为0,第三个参数甚至更多的参数是要向数组中插入的数据。
colors.splice(1,0,'red')
alert(colors) //yellow,red,blue
(3)替换:可以将已有的项替换,第一个参数为起始位置,第二个参数是要删除的项数,第三或者以后的参数为替换以后的数据
colors.splice(0,2,'black','green','brown')
alert(colors) //black,green,brown,blue
5.位置方法:indexOf()、lastIndexOf()
indexOf()和lastIndexOf()方法都接收两个参数,第一个参数为要在数组中查询的项,和要数组中查询时起始位置的索引(可选),返回值为要查询的值在数组中的索引位置,如果不存在返回-1,不同的是indexOf()默认是从索引位置为0的地方开始查询 ,而lastIndexOf()与前者正好相反。
const colors = ['red','blue','green','yellow'];
alert(colors.indexOf('green')) //2
alert(colors.lastIndexOf('green')) //1
如果查询到第一个符合条件的值时,就会停止查询,所以可以设置第二个参数,来规定从哪个索引开始查询
6.迭代方法:every()、filter()、forEach()、map()、some()
ES5中定义了5个迭代方法,它们都接受两个参数,第一个参数是要在每一项上运行的函数,第二个参数(可选)是运行该函数的作用域对象——影响this的值。传入这些方法的函数可以接受3个参数:数组项的值、该项在数组中的索引值、数组对象本身。 every():该方法对数组中的每一项运行给定的函数,如果每一项都返回true,则返回true.
const numbers = [1,2,3,4,5,6,7];
const everyResult = numbers.every(function(value, index, array){
return (value > 2);
});
alert(everyResult); //false
some():该方法与every()非常相似,some()方法只要对传入的函数对数组中的某一项返回true,则该方法返回true。
const someResult = numbers.some(function(value, index, array){
return (value > 2);
})
alert(someResult); //true
filter():对数组中的每一项运行给定的函数,返回该函数会返回true的项所组成的数组。
const filterResult = numbers.filter(function(value, index, array){
return (value > 2);
});
alert(filterResult); //3,4,5,6,7
map():该方法会返回一个新的数组,数组中的每一项都是在原数组中对应项上运行传入函数的结果。
const mapResult = numbers.map(function(value, index, array){
return (value + 1);
})
alert(mapResult); //2,3,4,5,6,7,8
forEach():该方法没有返回值,会改变原数组的值,它是对数组中的每一项运行传入 的函数。
nubers.forEach(function(value, index, array){
return (value * 2);
})
alert(numbers); //4,6,8,10,12,14,16
7.缩小方法:reduce()、reduceRight()
这两个数组都会迭代数组中的每一项,然后返回一个最终的值,不同的是reduce()方法从第一项开始遍历,而reduceRight()与前者正好相反。这两个方法都接收两个参数,一个是在每一项上调用的函数,第二个参数是作为缩小基础的初始值,而在每一项上调用的函数可以接受4个参数:前一个值、当前值、项的索引、数组对象。函数的返回值会作为第一个参数传给下一项。 第一次迭代发生在数组的第二项上,而第一个参数就是数组的第一项。
const reduceResult = numbers.reduce(function(prev, value, index, array){
return prev + value;
});
const reduceRightResult = numbers.reduce(function(prev, value, index, array){
return prev + value;
});
alert(reduceResult); //28
alert(reduceRightResult); //28
8.总结
不会影响原数组的方法: concat()、every()、some()、filter()、indexOf()、map()、slice() 会影响原数组的方法: pop()push()、shift()、unshift()、reverse()、sort()、splice()
写这些主要是自己对知识的一个梳理,因为总是对这些东西迷迷糊糊的,如果对读者有帮忙,那真的很荣幸。因为我的知识有限,所以总结的也不太好,望见谅。