在
之前的文章《一文講解JS中Object物件一些操作方法(分享)》中,給大家了解了JS中Object物件一些操作方法。以下這篇文章給大家了解JS中Array物件一些操作方法,有一定的參考價值,有需要的朋友可以參考一下,希望對你們有幫助。

javascript
中Array
一些高效的操作方法
Array.from()
方法從一個類似陣列或可迭代物件建立一個新的陣列實例。
1 2 3 4 | console.log(Array.from( "foo" ));
console.log(Array.from([1, 2, 3], (x) => x + x));
|
登入後複製
Array.isArray()
用於確定傳遞的值是否是一個Array
。
1 2 3 4 5 6 7 8 | Array.isArray([1, 2, 3]);
Array.isArray({ foo: 123 });
Array.isArray( "foobar" );
Array.isArray(undefined);
|
登入後複製
Array.obsolete()
用於非同步監視陣列發生的變化
已被廢棄語法:Array.observe( arr, callback)
Array.of()
方法建立一個具有可變數量參數的新陣列實例,而不考慮參數的數量或類型。
1 2 3 4 5 6 7 8 9 10 11 | Array.of(7);
Array.of(1, 2, 3);
Array(7);
Array(1, 2, 3);
if (!Array.of) {
Array.of = function () {
return Array.prototype.slice.call(arguments);
};
}
|
登入後複製
Array.concat()
方法用於合併兩個或多個陣列。此方法不會變更現有數組,而是傳回一個新數組。
1 2 3 4 5 | var array1 = [ "a" , "b" , "c" ];
var array2 = [ "d" , "e" , "f" ];
console.log(array1.concat(array2));
|
登入後複製
Array.copyWithin()
方法淺複製陣列的一部分到同一陣列中的另一個位置,並傳回它,而不修改其大小。
1 2 3 4 5 6 7 8 9 | var array1 = [1, 2, 3, 4, 5];
console.log(array1.copyWithin(0, 3, 4));
console.log(array1.copyWithin(1, 3));
|
登入後複製
Array.entries()
方法傳回一個新的Array Iterator
對象,該物件包含數組中每個索引的鍵/值對。
1 2 3 4 5 6 7 8 9 | var array1 = [ "a" , "b" , "c" ];
var iterator1 = array1.entries();
console.log(iterator1.next().value);
console.log(iterator1.next().value);
|
登入後複製
Array.every()
方法測試陣列的所有元素是否都通過了指定函數的測試。
1 2 3 | var array1 = [1, 30, 39, 29, 10, 13];
console.log(array1.every((x) => x < 40));
|
登入後複製
Array.fill()
方法用一個固定值填入一個陣列中從起始索引到終止索引內的全部元素。不包括終止
1 2 3 4 5 6 7 8 9 | var array1 = [1, 2, 3, 4];
console.log(array1.fill(0, 2, 4));
console.log(array1.fill(5, 1));
console.log(array1.fill(6));
|
登入後複製
Array.filter()
方法來建立一個新數組,其包含透過所提供函數實現的測試的所有元素。
1 2 3 4 5 6 | var words = [ "spray" , "limit" , "elite" , "exuberant" , "destruction" , "present" ];
const result = words.filter((word) => word.length > 6);
console.log(result);
|
登入後複製
Array.find()
方法傳回數組中滿足提供的測試函數的第一個元素的值。否則返回undefined
。
1 2 3 4 5 6 | var array1 = [5, 12, 8, 130, 44];
var found = array1.find((x) => x > 10);
console.log(found);
|
登入後複製
Array.findIndex()
方法傳回數組中滿足提供的測試函數的第一個元素的索引。否則返回-1
。
1 2 3 4 5 6 | var array1 = [5, 12, 8, 130, 44];
var index = array1.findIndex((x) => x > 10);
console.log(index);
|
登入後複製
Array.flat()
方法會遞歸到指定深度將所有子數組連接,並傳回一個新數組。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var arr1 = [1, 2, [3, 4]];
arr1.flat();
var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
var arr4 = [1, 2, , 4, 5];
arr4.flat();
|
登入後複製
Array.flatMap()
方法首先使用映射函數來對應每個元素,然後將結果壓縮成一個新陣列。它與map
和深度值1
的flat
幾乎相同,但flatMap
通常在合併成一種方法的效率稍微高一些。
1 2 3 4 5 6 7 8 9 10 11 | var arr1 = [1, 2, 3, 4];
arr1.map((x) => [x * 2]);
arr1.flatMap((x) => [x * 2]);
arr1.flatMap((x) => [[x * 2]]);
|
登入後複製
Array.forEach()
方法對陣列的每個元素執行一次提供的函數。
1 2 3 4 5 6 | var array1 = [ "a" , "b" , "c" ];
array1.forEach((value, index, arr) => console.log(value));
|
登入後複製
Array.includes(value,index)
方法用來判斷一個陣列是否包含一個指定的值,根據情況,如果包含則傳回true
,否則返回false
。
1 2 3 4 5 6 7 8 9 10 11 12 | var array1 = [1, 2, 3];
console.log(array1.includes(2));
var pets = [ "cat" , "dog" , "bat" ];
console.log(pets.includes( "cat" ));
console.log(pets.includes( "at" ));
|
登入後複製
Array.indexOf()
方法傳回在陣列中可以找到一個給定元素的第一個索引,如果不存在,則傳回-1
。
1 2 3 4 5 6 7 8 9 10 11 | / var beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
console.log(beasts.indexOf('bison', 2));
console.log(beasts.indexOf('giraffe'));
|
登入後複製
Array.join()
方法將一個陣列(或一個類別數組物件)的所有元素連接成一個字串並傳回這個字元
1 2 3 4 5 6 7 8 9 10 11 12 13 | var elements = [ "Fire" , "Wind" , "Rain" ];
console.log(elements.join());
console.log(elements.join( "" ));
console.log(elements.join( "-" ));
eval ([1, 2, 3, 3, 4, 5].join( "+" )) = 18;
|
登入後複製
Array. keys()
方法傳回一個新的Array
迭代器,它包含陣列中每個索引的鍵。
1 2 3 4 5 6 | var array1 = [ "a" , "b" , "c" ];
var iterator = array1.keys();
for (let key of iterator) {
console.log(key);
}
|
登入後複製
Array.lastIndexOf(item,index)
方法傳回指定元素(也即有效的JavaScript
值或變數)在陣列中的最後一個的索引,如果不存在則回傳-1
。從陣列的後面向前查找,從fromIndex
處開始。
1 2 3 4 5 6 7 | var animals = [ "Dodo" , "Tiger" , "Penguin" , "Dodo" ];
console.log(animals.lastIndexOf( "Dodo" ));
console.log(animals.lastIndexOf( "Tiger" ));
|
登入後複製
Array.map()
方法建立一個新數組,其結果是該數組中的每個元素都呼叫一個提供的函數後傳回的結果。
1 2 3 4 5 6 7 | var array1 = [1, 4, 9, 16];
const map1 = array1.map((x) => x * 2);
console.log(map1);
|
登入後複製
Array.pop()
方法從陣列中刪除最後一個元素,並傳回該元素的值。此方法會變更數組的長度。
1 2 3 4 5 6 7 8 | var plants = [ "broccoli" , "cauliflower" , "cabbage" , "kale" , "tomato" ];
console.log(plants.pop());
console.log(plants);
plants.pop();
console.log(plants);
|
登入後複製
Array.push()
方法將一個或多個元素新增到陣列的結尾,並傳回新陣列的長度。
1 2 3 4 5 6 7 8 9 10 11 12 | var animals = [ "pigs" , "goats" , "sheep" ];
console.log(animals.push( "cows" ));
console.log(animals);
animals.push( "chickens" );
console.log(animals);
|
登入後複製
Array.reduce()
方法對累加器和陣列中的每個元素(從左到右)套用函數,將其減少為單一值。
1 2 3 4 5 6 7 8 9 10 | const array1 = [1, 2, 3, 4];
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(array1.reduce(reducer));
console.log(array1.reduce(reducer, 5));
|
登入後複製
Array.reduceRight()
方法接受一個函數作為累加器(accumulator
)和數組的每個值(從右到左)將其減少為單一值。
1 2 3 4 5 6 7 8 | const array1 = [
[0, 1],
[2, 3],
[4, 5],
].reduceRight((accumulator, currentValue) => accumulator.concat(currentValue));
console.log(array1);
|
登入後複製
Array.reverse()
方法將陣列中元素的位置顛倒。
1 2 3 4 5 6 7 8 9 10 11 12 13 | var array1 = [ "one" , "two" , "three" ];
console.log( "array1: " , array1);
var reversed = array1.reverse();
console.log( "reversed: " , reversed);
console.log( "array1: " , array1);
|
登入後複製
Array.shift()
方法從陣列中刪除第一個元素,並傳回該元素的值。此方法會變更數組的長度。
1 2 3 4 5 6 7 8 9 | var array1 = [1, 2, 3];
var firstElement = array1.shift();
console.log(array1);
console.log(firstElement);
|
登入後複製
Array.slice()
方法傳回一個從開始到結束(不包括結束)選擇的陣列的一部分淺拷貝到一個新陣列物件。且原始數組不會被修改。
1 2 3 4 5 6 7 8 9 10 | var animals = [ "ant" , "bison" , "camel" , "duck" , "elephant" ];
console.log(animals.slice(2));
console.log(animals.slice(2, 4));
console.log(animals.slice(1, 5));
|
登入後複製
Array.some()
方法測試陣列中的某些元素是否透過由提供的函數實現的測試。
1 2 3 4 5 6 7 8 9 | var array = [1, 2, 3, 4, 5];
var even = function (element) {
return element % 2 === 0;
};
console.log( array .some(even));
|
登入後複製
Array.sort()
方法用原地算法对数组的元素进行排序,并返回数组。排序不一定是稳定的。默认排序顺序是根据字符串Unicode
码点。
1 2 3 4 5 6 7 8 9 | var months = [ "March" , "Jan" , "Feb" , "Dec" ];
months.sort();
console.log(months);
var array1 = [1, 30, 4, 21];
array1.sort();
console.log(array1);
|
登入後複製
Array.splice()
方法通过删除现有元素和/或添加新元素来更改一个数组的内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | var months = [ "Jan" , "March" , "April" , "June" ];
months.splice(1, 0, "Feb" );
console.log(months);
months.splice(4, 1, "May" );
console.log(months);
months.splice(4, 1);
console.log(months);
|
登入後複製
Array.toLocaleString()
返回一个字符串表示数组中的元素。数组中的元素将使用各自的toLocaleString
方法转成字符串,这些字符串将使用一个特定语言环境的字符串(例如一个逗号 ",
")隔开。
1 2 3 4 5 6 7 8 9 10 | var array1 = [1, "a" , new Date ( "21 Dec 1997 14:12:00 UTC" )];
var localeString = array1.toLocaleString( "en" , { timeZone: "UTC" });
console.log(localeString);
var prices = [ "¥7" , 500, 8123, 12];
prices.toLocaleString( "ja-JP" , { style: "currency" , currency: "JPY" });
|
登入後複製
Array.toSource()
返回一个字符串,代表该数组的源代码。
该特性是非标准的,请尽量不要在生产环境中使用它!
1 2 3 | var alpha = new Array( "a" , "b" , "c" );
alpha.toSource();
|
登入後複製
Array.toString()
返回一个字符串,表示指定的数组及其元素。
1 2 3 4 | var array1 = [1, 2, "a" , "1a" ];
console.log(array1.toString());
|
登入後複製
Array.unshift()
方法将一个或多个元素添加到数组的开头,并返回新数组的长度。
1 2 3 4 5 6 7 | var array1 = [1, 2, 3];
console.log(array1.unshift(4, 5));
console.log(array1);
|
登入後複製
Array.values()
方法返回一个新的Array Iterator
对象,该对象包含数组每个索引的值。
1 2 3 4 5 6 7 | const array1 = [ "a" , "b" , "c" ];
const iterator = array1.values();
for ( const value of iterator) {
console.log(value);
}
|
登入後複製
推荐学习:JavaScript视频教程
以上是淺析JS中Array物件一些操作方法(附程式碼)的詳細內容。更多資訊請關注PHP中文網其他相關文章!