1. push() 方法
????push() 方法將一個或多個元素添加到數組的末尾,并返回該數組的新長度望忆。
var animals = ['pigs', 'goats', 'sheep'];
console.log(animals.push('cows'));
// expected output: 4
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows"]
animals.push('chickens');
console.log(animals);
// expected output: Array ["pigs", "goats", "sheep", "cows", "chickens"]
2. sort() 方法
????sort() 方法用原地算法對數組的元素進行排序均函,并返回數組坦喘。排序算法現在是穩(wěn)定的乍构。默認排序順序是根據字符串Unicode碼點绰精。
由于它取決于具體實現哑诊,因此無法保證排序的時間和空間復雜性躬络。
var months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// expected output: Array ["Dec", "Feb", "Jan", "March"]
var array1 = [1, 30, 4, 21];
array1.sort((a, b) => a - b);
console.log(array1);
// expected output: Array [1, 4, 21, 30]
3. indexOf() 方法
????indexOf()方法返回在數組中可以找到一個給定元素的第一個索引,如果不存在搭儒,則返回-1穷当。
var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
// start from index 2
console.log(beasts.indexOf('bison', 2));
// expected output: 4
console.log(beasts.indexOf('giraffe'));
// expected output: -1
4. includes() 方法
????includes() 方法用來判斷一個數組是否包含一個指定的值提茁,根據情況,如果包含則返回 true馁菜,否則返回false代碼如下:
var array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
var pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('at'));
// expected output: false
5. isArray()方法
????Array.isArray() 用于確定傳遞的值是否是一個 Array
茴扁。
Array.isArray([1, 2, 3]);
// true
Array.isArray({foo: 123});
// false
Array.isArray("foobar");
// false
Array.isArray(undefined);
// false
6. join方法
????join() 方法將一個數組(或一個類數組對象)的所有元素連接成一個字符串并返回這個字符串。
var elements = ['Fire', 'Wind', 'Rain'];
console.log(elements.join());
// expected output: Fire,Wind,Rain
console.log(elements.join(''));
// expected output: FireWindRain
console.log(elements.join('-'));
// expected output: Fire-Wind-Rain
7. pop()方法
????pop()方法從數組中刪除最后一個元素汪疮,并返回該元素的值峭火。此方法更改數組的長度。
var plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop());
// expected output: "tomato"
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage", "kale"]
plants.pop();
console.log(plants);
// expected output: Array ["broccoli", "cauliflower", "cabbage"]
8. reverse()方法
????reverse() 方法將數組中元素的位置顛倒智嚷。第一個數組元素成為最后一個數組元素卖丸,最后一個數組元素成為第一個。
var array1 = ['one', 'two', 'three'];
console.log('array1: ', array1);
// expected output: Array ['one', 'two', 'three']
var reversed = array1.reverse();
console.log('reversed: ', reversed);
// expected output: Array ['three', 'two', 'one']
/* 它也改變原來的數組 */
console.log('array1: ', array1);
// expected output: Array ['three', 'two', 'one']
9. shift()方法
????shift() 方法從數組中刪除第一個元素盏道,并返回該元素的值稍浆。此方法更改數組的長度。
var array1 = [1, 2, 3];
var firstElement = array1.shift();
console.log(array1);
// expected output: Array [2, 3]
console.log(firstElement);
// expected output: 1
10. unshift()方法
????unshift() 方法將一個或多個元素添加到數組的開頭猜嘱,并返回該數組的新長度衅枫。
var array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
// expected output: 5
console.log(array1);
// expected output: Array [4, 5, 1, 2, 3]
11. slice()方法
????slice() 方法返回一個新的數組對象,這一對象是一個由 開始和 結束(不包括結束)決定的原數組的淺拷貝朗伶。原始數組不會被改變弦撩。
var animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
console.log(animals.slice(1, 5));
// expected output: Array ["bison", "camel", "duck", "elephant"]
12. splice()方法
????splice()方法通過刪除現有元素和/或添加新元素來修改數組,并以數組返回原數組中被修改的內容。
var months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// 插入第一個索引位置
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'June']
months.splice(4, 1, 'May');
// 替換第4索引處的1個元素
console.log(months);
// expected output: Array ['Jan', 'Feb', 'March', 'April', 'May']
13. tostring()方法
????toString() 返回一個字符串论皆,表示指定的數組及其元素益楼。
var array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// expected output: "1,2,a,1a"