1、队尾插进去push
var colors = ["red","green"];
colors.push("black"):
console.log(colors); //["red","green","black"]
2、队尾删除并返回删除的末了一项pop
var colors = ["red","green","black"];
var item = colors.pop();
console.log(item); //"black"
3、队首插进去unshift()
var colors = ["red","green"];
colors.unshift("black");
var item = colors.pop();
console.log(item); //"black"
4、队首删除shift()
var colors = ["red","green","black"];
colors.shift();
console.log(colors); //["green","black"]
5、数组一增加数组二concat()
var colors = ["red","green","black"];
var colors2 = colors.concat("yellow",["blue","brown"]);
console.log(colors); //["red","green","black"]
console.log(colors2); //["red","green","black","yellow","blue","brown"]
6、数组的截取slice()
只传一个参数:从数组这个参数的下标最先截取一直到数组完毕。
var colors = ["red","green","black"];
colors.slice(1); //["green","black"]
console.log(colors); //["red","green","black"]
传两个参数:第一个是截取最先的位置,第二个是截取完毕的位置
var colors = ["red","green","black","yellow","blue","brown"];
colors.slice(1,3)//从位置1最先,到位置2完毕["green","black"];
7、数组的splice()要领
有三种用法:
- 删除:能够删除恣意数目的项,只需指定两个参数:第一个参数为删除最先的位置,第二个参数为删除项数。
- 插进去:能够向指定位置插进去恣意数目的项,只需供应3个参数:肇端位置、0(要删除的项数)和要插进去的项。比方:splice(2,0,”red”,”green”),会从当前数组的位置2最先插进去字符串”red”和”green”。
- 替代: 能够向指定位置插进去恣意数目的项,且同事删除恣意数目的项,只需供应3个参数:肇端位置、要删除的项数和要插进去的恣意数目的项。插进去的项数没必要与删除项数相称。比方:splice (2,1,”red”,”green”) 会删除当前数组位置2的项,然后再从位置2最先插进去字符串。
var colors = ["red","green","black"];
var removed = colors.splice(0,1);
console.log(colors); //["green","black"]
console.log(removed); //["red"]
removed = colors.splice(1,0,"yellow","orange");
console.log(colors); //["green","yellow","orange","black"]
console.log(removed); //[]
removed = colors.splice(1,1,"red","purple");
console.log(colors); //["green","red","purple","orange","black"]
console.log(removed); //["yellow"]
8、位置要领indexOf()和lastIndexOf()
indexOf()和lastIndexOf()都吸收两个参数,第一个参数是要查找的项,第二个(可选)查找最先的位置,indexOf()是从数组头最先查,lastIndexOf()是从数组尾最先查找。
var numbers = [1,2,3,4,5,4,3,2,1];
console.log(numbers.indexOf(4)); //3
console.log(numbers.lastIndexOf(4)); //5
console.log(numbers.indexOf(4,4)); //5
var person = {name: "vivi"};
var people = [{name: "vivi"}];
var morePeople = [person];
console.log(people.indexOf(person)); //-1
console.log(morePeople.indexOf(person)); //0
9、查找find()要领
查找相符前提的第一项
var inventory = [
{name: 'apples', quantity: 2},
{name: 'bananas', quantity: 0},
{name: 'cherries', quantity: 5}
];
const inventorItem = inventory.find((item) => item.name === 'apples');
console.log(inventorItem); //{name: 'apples', quantity: 2}
10、迭代要领
传入这些要领中的函数会吸收三个参数:数组项的值、该项在数组中的位置和数组对象自身。
- every(): 对数组中的每一项运转给定函数,假如该函数对每一项都返回true,则返回true。
- filter(): 对数组中的每一项运转给定函数,返回该函数会返回true的项构成的数组。
- forEach(): 对数组中的每一项运转给定函数,这个要领没有返回值。
- map(): 对数组中的每一项运转给定函数,返回每次函数挪用的效果构成的数组。
- some(): 对数组中的每一项运转给定函数,假如该函数对任一项返回true,则返回true。
var numbers = [1,2,3,4,5,4,3,2,1];
var everyResult = numbers.every((item, index, array) => {
return item > 2;
});
console.log(everyResult); //false
var someResult = numbers.some((item, index, array) => {
return item > 2;
});
console.log(someResult); //true
var numbers = [1,2,3,4,5,4,3,2,1];
var filterResult = numbers.filter((item, index, array) => {
return item > 2;
});
console.log(filterResult); //[3,4,5,4,3]
var numbers = [1,2,3,4,5,4,3,2,1];
var mapResult = numbers.map((item, index, array) => {
return item * 2;
});
console.log(mapResult); //[2,4,6,8,10,8,6,4,2]
var numbers = [1,2,3,4,5,4,3,2,1];
numbers.forEach((item, index, array) => {
//实行某些操纵
});
11、合并要领reduce()和reduceRight()
这两个要领都邑迭代数组的一切项,然后构建一个终究返回的值。
reduce()要领从数组的第一项最先,逐一遍历到末了。
reduceRight()则从数组的末了一项最先,向前遍历到第一项。
var values = [1,2,3,4,5];
var sum = values.reduce((prev, cur, index, array) => {
return prev + cur;
});
console.log(sum); //15
var values = [1,2,3,4,5];
var sum = values.reduceRight((prev, cur, index, array) =>
{
return prev + cur;
});
console.log(sum); //15