JavaScript 的 Array 對(duì)象是用于構(gòu)造數(shù)組的全局對(duì)象,數(shù)組是類似于列表的高階對(duì)象。
let fruits = ['Apple', 'Banana'];
concat() 方法
用于合并兩個(gè)或多個(gè)數(shù)組,返回合并后的新數(shù)組。
const a = [1, 2, 3];
const b = [4, 5, 6];
const c = a.concat(b);
console.log(c); // expected output: [ 1, 2, 3, 4, 5, 6 ]
every() 方法
用于判斷一個(gè)數(shù)組內(nèi)的所有元素是否都能通過(guò)某個(gè)指定函數(shù)的測(cè)試。返回一個(gè)布爾值。
const a = [1, 2, 3];
let b = a.every(item => item > 0);
console.log(b); // expected output: true
fill() 方法
用指定值填充數(shù)組中從起始索引到終止索引內(nèi)的全部元素。該方法會(huì)改變?cè)瓟?shù)組。參數(shù)分別為: 指定值,起始索引(可選,默認(rèn)為 0)和終止索引(可選,默認(rèn)為 array.length)。
const a = [1, 2, 3];
const b = a.fill(0, 0, 3);
console.log(a); // expected output: [ 0, 0, 0 ]
console.log(b); // expected output: [ 0, 0, 0 ]
filter() 方法
返回?cái)?shù)組中包含符合指定條件的所有元素的新數(shù)組。
const a = [1, 2, 3];
const b = a.filter(item => item > 1);
console.log(a); // expected output: [ 1, 2, 3 ]
console.log(b); // expected output: [ 2, 3 ]
find() 方法
返回?cái)?shù)組中符合指定條件的第一個(gè)元素或 undefined。
const a = [1, 2, 3];
const b = a.find(item => item > 1);
const c = a.find(item => item > 3);
console.log(b); // expected output: 2
console.log(c); // expected output: undefined
findIndex() 方法
返回?cái)?shù)組中符合指定條件的第一個(gè)元素的索引或 -1。
const a = [1, 2, 3];
const b = a.findIndex(item => item > 1);
const c = a.findIndex(item => item > 3);
console.log(b); // expected output: 1
console.log(c); // expected output: -1
forEach() 方法
對(duì)數(shù)組的每個(gè)元素執(zhí)行一次給定的函數(shù)。沒(méi)有返回值。除了拋出異常以外,沒(méi)有辦法中止或跳出 forEach() 循環(huán)。
const a = [1, 2, 3];
let b = 0;
const c = a.forEach(item => b += item);
console.log(a); // expected output: [ 1, 2, 3 ]
console.log(b); // expected output: 6
console.log(c); // expected output: undefined
includes() 方法
用來(lái)判斷一個(gè)數(shù)組是否包含一個(gè)指定的值,返回一個(gè)布爾值。
const a = [1, 2, 3];
const b = a.includes(1);
console.log(b); // expected output: true
indexOf() 方法
返回?cái)?shù)組中指定元素的第一次出現(xiàn)的索引或 -1。返回最后一次出現(xiàn)的可以使用 lastIndexOf() 方法。
const a = [1, 2, 3];
const b = a.indexOf(2);
console.log(b); // expected output: 1
join() 方法
將一個(gè)數(shù)組的所有元素通過(guò)分隔符(默認(rèn)為 ,)連接成一個(gè)字符串,并返回這個(gè)字符串。
const a = [1, 2, 3];
const b = a.join();
const c = a.join(" + ");
console.log(b); // expected output: 1,2,3
console.log(c); // expected output: 1 + 2 + 3
map() 方法
返回一個(gè)新數(shù)組,數(shù)組中的元素為原始數(shù)組元素調(diào)用函數(shù)處理后的值。
// 示例一
const a = [1, 2, 3];
const b = a.map(item => item * 2);
console.log(a); // expected output: [ 1, 2, 3 ]
console.log(b); // expected output: [ 2, 4, 6 ]
// 示例二
const c = [{ id: 1 }, { id: 2 }, { id: 3 }];
const d = c.map(item => item.id);
console.log(c); // expected output: [ { id: 1 }, { id: 2 }, { id: 3 } ]
console.log(d); // expected output: [ 1, 2, 3 ]
pop() 方法
刪除并返回?cái)?shù)組中的最后一個(gè)元素。該方法會(huì)改變?cè)瓟?shù)組。
const a = [1, 2, 3];
const b = a.pop();
console.log(b); // expected output: 3
console.log(a); // expected output: [ 1, 2 ]
push() 方法
將一個(gè)或多個(gè)元素添加到數(shù)組的末尾,并返回該數(shù)組的新長(zhǎng)度。
const a = [1, 2, 3];
const b = a.push(4, 5);
console.log(a); // expected output: [ 1, 2, 3, 4, 5 ]
console.log(b); // expected output: 5
reduce() 方法
從左到右依次對(duì)數(shù)組中的元素執(zhí)行傳入的函數(shù),將其結(jié)果匯總并返回。從右到左可以使用 reduceRight() 方法。
const a = [1, 2, 3];
const b = a.reduce((total, current) => {
return total + current;
});
console.log(b); // expected output: 6
reverse() 方法
將數(shù)組中元素的位置顛倒,并返回該數(shù)組。該方法會(huì)改變?cè)瓟?shù)組。
const a = [1, 2, 3];
const b = a.reverse();
console.log(a); // expected output: [ 3, 2, 1 ]
console.log(b); // expected output: [ 3, 2, 1 ]
shift() 方法
刪除并返回?cái)?shù)組中的第一個(gè)元素。該方法會(huì)改變?cè)瓟?shù)組。
const a = [1, 2, 3];
const b = a.shift();
console.log(a); // expected output: [ 2, 3 ]
console.log(b); // expected output: 1
slice() 方法
返回一個(gè)從起始索引(可選,默認(rèn)為 0)到終止索引(可選,默認(rèn)為 array.length + 1)內(nèi)的新數(shù)組。
const a = [1, 2, 3, 4, 5];
const b = a.slice(0, 3);
const c = a.slice();
const d = a.slice(0, 5);
console.log(b); // expected output: [ 1, 2, 3 ]
console.log(c); // expected output: [ 1, 2, 3, 4, 5 ]
console.log(d); // expected output: [ 1, 2, 3, 4, 5 ]
some() 方法
用于判斷數(shù)組內(nèi)是否包含能通過(guò)某個(gè)指定函數(shù)的測(cè)試的元素。返回一個(gè)布爾值。
const a = [1, 2, 3];
const b = a.some(item => item % 2 === 0);
console.log(b); // expected output: true
sort() 方法
對(duì)數(shù)組的元素進(jìn)行排序,并返回?cái)?shù)組。該方法會(huì)改變?cè)瓟?shù)組。
const a = [3, 5, 4, 2, 1];
const b = [3, 5, 4, 2, 1];
const c = [3, 5, 4, 2, 1];
const d = a.sort();
const e = b.sort((x, y) => x - y);
const f = c.sort((x, y) => y - x);
console.log(d); // expected output: [ 1, 2, 3, 4, 5 ]
console.log(e); // expected output: [ 1, 2, 3, 4, 5 ]
console.log(f); // expected output: [ 5, 4, 3, 2, 1 ]
splice() 方法
從起始索引(start,第一個(gè)參數(shù))刪除、替換或添加若干個(gè)(count,第二個(gè)參數(shù),可選,默認(rèn)為 array.length - start)元素,并以數(shù)組形式返回被刪除或被替換的元素。count 之后的參數(shù)為要添加或替換的元素。該方法會(huì)改變?cè)瓟?shù)組。
// 刪除
const a = [1, 2, 3, 4, 5];
const b = a.splice(0, 3);
console.log(a); // expected output: [ 4, 5 ]
console.log(b); // expected output: [ 1, 2, 3 ]
// 替換
const c = [1, 2, 3, 4, 5];
const d = c.splice(0, 2, "one", "two");
console.log(c); // expected output: [ 'one', 'two', 3, 4, 5 ]
console.log(d); // expected output: [ 1, 2 ]
// 添加
const e = [1, 2, 3, 4, 5];
const f = e.splice(5, 5, 6, 7);
console.log(e); // expected output: [ 1, 2, 3, 4, 5, 6, 7 ]
console.log(f); // expected output: []
unshift() 方法
將一個(gè)或多個(gè)元素添加到數(shù)組的開(kāi)頭,并返回該數(shù)組的新長(zhǎng)度。該方法會(huì)改變?cè)瓟?shù)組。
const a = [3, 4, 5];
const b = a.unshift(1, 2);
console.log(a); // expected output: [ 1, 2, 3, 4, 5 ]
console.log(b); // expected output: 5