錯(cuò)誤之處初狰,歡迎指正秫筏,持續(xù)更新中诱鞠。
1. 方法
1. reverse
讓數(shù)組逆序排序,會(huì)改變?cè)瓟?shù)組这敬。
const arr = [1, 2, 3, 4, 5];
console.log(arr); //[ 1, 2, 3, 4, 5 ]
const reverseArr = arr.reverse();
console.log(arr); //[ 5, 4, 3, 2, 1 ]
console.log(reverseArr); //[ 5, 4, 3, 2, 1 ]
2. toString
將一個(gè)數(shù)組轉(zhuǎn)換成字符串航夺,不會(huì)改變?cè)瓟?shù)組。
const arr = [1, 2, 3, 4, 5];
console.log(arr); //[ 1, 2, 3, 4, 5 ]
const string = arr.toString();
console.log(arr); //[ 1, 2, 3, 4, 5 ]
console.log(string); //1,2,3,4,5
3. slice
截取數(shù)組崔涂,不會(huì)改變?cè)瓟?shù)組阳掐。
const arr = ["a", "b", 1, 2, 3, 4, 5];
console.log(arr); //["a", "b", 1, 2, 3, 4, 5]
const newArr = arr.slice(2, 7);
console.log(arr); //["a", "b", 1, 2, 3, 4, 5]
console.log(newArr); //[ 1, 2, 3, 4, 5 ]
關(guān)于slice
方法的參數(shù)(以上述為例):
arr.slice(2,7)
就是截取數(shù)組arr
的arr[2]
到arr[6]
。
如果只填寫(xiě)一個(gè)參數(shù)arr.slice(2)
冷蚂,就是截取數(shù)組arr
的arr[2]
到arr[length-1]
缭保。
4. push
和pop
push
方法可以用來(lái)在數(shù)組末尾添加一個(gè)或者多個(gè)元素,返回值為數(shù)組長(zhǎng)度蝙茶。
pop
方法可以用來(lái)刪除數(shù)組的最后一個(gè)元素艺骂,返回值為被刪除的元素。
var a =['a','b','c','d'];
console.log(a.push('e')); //5
console.log(a); //["a", "b", "c", "d", "e"]
console.log(a.pop()); //e
console.log(a); //["a", "b", "c", "d"]
5. join
將數(shù)組轉(zhuǎn)換成字符串隆夯,并以指定字符相連(默認(rèn)為,
)钳恕。
const arr = [1, 2, 3, 4, 5];
const newString = arr.join('-');
console.log(arr); //[1,2,3,4,5]
console.log(newArr);//1-2-3-4-5
6. indexOf
用來(lái)判斷數(shù)組中是否包含目標(biāo)值。
const arr = [1, 2, 3, 4, 5];
console.log(arr.indexOf(5)); //4
console.log(arr.indexOf(6)); //-1
如果數(shù)組中包含目標(biāo)數(shù)值蹄衷,返回的是目標(biāo)數(shù)值在數(shù)組中的索引值忧额;如果不包含返回-1
。
7. map
const arr = [1,2,3,4,5];
const newArr = arr.map((item) => {
return item;
})
console.log(arr); //[1,2,3,4,5]
console.log(newArr); //[1,2,3,4,5]
console.log(arr === newArr); //控制臺(tái)輸出為false
不會(huì)改變?cè)袛?shù)組宦芦,不光可以使用map
來(lái)映射一個(gè)數(shù)組中的值,還可用來(lái)進(jìn)行一些進(jìn)階操作轴脐,例如:
const arr = [1,2,3,4,5];
const newArr = arr.map((item) => {
return item * 10;
})
console.log(arr); //[1,2,3,4,5]
console.log(newArr); //[10,20,30,40,50]
8. filter
不會(huì)改變?cè)瓟?shù)組调卑,一般用來(lái)過(guò)濾數(shù)組中的某些項(xiàng)抡砂。
const arr = [1,2,3,4,5];
const newArr = arr.filter((item) => {
return item > 2;
})
console.log(arr); //[1,2,3,4,5]
console.log(newArr); //[3,4,5]
9. includes(ES6)
用來(lái)判斷數(shù)組中是否包含目標(biāo)值。
const arr = [1, 2, 3, 4, 5];
console.log(arr.includes(5)); //true
console.log(arr.includes(6)); //false
如果數(shù)組中包含目標(biāo)數(shù)值返回true
恬涧;如果不包含返回false
注益。
10. find(ES6)
const arr = [1, 2, 3, 4, 5];
const found = arr.find(element => element > 2);
console.log(arr); //[1,2,3,4,5]
console.log(found); //3
find
方法返回?cái)?shù)組中滿足條件的第一個(gè)元素的值,例如上述的條件就是值大于2
溯捆,所以返回了3
丑搔;如果沒(méi)有滿足條件的項(xiàng),返回undefined
提揍。
11. forEach
const arr = [1, 2, 3, 4, 5];
const newArr = [];
arr.forEach(element => {
newArr.push(element * 10)
});
console.log(arr); //[1,2,3,4,5]
console.log(newArr); //[10,20,30,40,50]
forEach
方法對(duì)指定數(shù)組的每個(gè)元素執(zhí)行一次給定的函數(shù)啤月,例如上述就是把arr
數(shù)組里面每個(gè)值乘10
,然后push
到newArr
數(shù)組里劳跃。
12. fill(ES6)
將數(shù)組的每一項(xiàng)填充為給定的值谎仲。
const arr = new Array(100); //創(chuàng)建一個(gè)長(zhǎng)度為100的數(shù)組
arr.fill('chris');
13. concat
用于合并兩個(gè)或多個(gè)數(shù)組,不會(huì)改變?cè)瓟?shù)組刨仑。
const arr1 = [1,2,3,4,5];
const arr2 = ['a','c'];
const newArr = arr1.concat(arr2);
console.log(arr1); //[1,2,3,4,5]
console.log(arr2); //['a','c']
console.log(newArr);//[1,2,3,4,5,'a','c']
2. 屬性
1. length
返回?cái)?shù)組的長(zhǎng)度郑诺。
const arr = [1, 2, 3, 4, 5];
console.log(arr.length); //5