JS-數組經常使用方法整理

想了解數組有哪些原生方法,控制檯輸出,如圖:數組

 

 

 

length:數組的實例屬性,返回或設置一個數組中的元素個數。函數

 

toString():能夠把數組轉換成字符串,並返回結果。測試

toLocaleString():返回數組中每一個元素的本地化表示形式。spa

let a = [1234,'hello',21];
a.length; //3
a.toString();  //'1234','hello','21'
a.toLocaleString();  //'1,234','hello','21'

 

join([seperator]):返回一個字符串,由每一個元素轉換而成的字符串使用指定的seperator拼接而成。3d

let a = [1234,'hello',21];
a.join('-'); //'1234-hello-21'

 

concat():拼接兩個或更多的數組,並返回結果。此方法不會更改現有數組,而是返回一個新數組。每一個參數能夠是一個值,也能夠是一個數組,能夠接收任意多個參數。可用做拷貝數組。code

let a = [1234,'hello',21];
let b = a.concat(50,60,[1,2,3]); //[1234, "hello", 21, 50, 60, 1, 2, 3]

 

reverse():顛倒數組中元素的順序,現有數組內容會改變。對象

let a = [1234,'hello',21];
let b = a.reverse();
a; //[21, "hello", 1234]
b; //[21, "hello", 1234]

 

sort():講數組元素按Unicode碼進行從新排序。blog

let a = [1234,'hello',21,'bar',6,789];
a.sort();
a; //[1234, 21, 6, 789, "bar", "hello"]

 

slice(start, [end]) : 返回現有數組的一個子數組,從下標爲start的位置開始取,到下標爲end的爲止(獲取內容不包括end),若參數爲負數,則表示從尾部開始算起,end參數能夠省略。可用做拷貝數組。排序

let a = [1234,'hello',21,'bar',6,789];
let b = a.slice(1,4); //["hello", 21, "bar"]
let c = a.slice(2); //[21, "bar", 6, 789]
let d = a.slice(-4,-2); //[21, "bar"]
a; //[1234, "hello", 21, "bar", 6, 789]

 

splice(start,count,e1,e2,...en):從數組中刪除一部分元素,並添加另外一部分元素,start爲指定添加/刪除的起始位置,可取負值,count爲要刪除的個數,0表示不刪除,e1,e2表示在start新添加的項,返回會刪除的項目,現有數組會改變。遞歸

let a = [10,20,30,40,50];
let b = a.splice(2,2,110);
b; //[30,40]
a; //[10,20,110,50,]
let c = a.splice(2,0,[70,80,90]);
c; //[]
a; //[10,20,[70,80,90],110,50]

 

push():將一個或多個元素添加到數組的末尾,並返回該數組的新長度。

unshift():將一個或多個元素添加到數組的開頭,並返回該數組的新長度。

pop():從數組中刪除最後一個元素,並返回該元素的值。此方法更改數組的長度。

shift():從數組中刪除第一個元素,並返回該元素的值。此方法更改數組的長度。

let a = [10,20,30,40,50];
let a1 = a.push(60);
a1; //6
a; //[10,20,30,40,50,60]

let b = [10,20,30,40,50];
let b1 = b.unshift(60);
b1; //6
b; //[60,10,20,30,40,50]

let c = [10,20,30,40,50];
let c1 = c.pop();
c1; //50
c; //[10,20,30,40]

let d = [10,20,30,40,50];
let d1 = d.shift();
d1; //10
d; //[20,30,40,50]

 

includes():用來判斷一個數組是否包含一個指定的值,根據狀況,若是包含則返回 true,不然返回false。

let array1 = [1, 2, 3];
array1.includes(2); //true

 

indexOf(value, [fromIndex]):返回在數組中能夠找到一個給定元素的第一個索引,若是不存在,則返回-1。fromIndex爲可選,指定從該下標處開始進行查找。

lastIndexOf(value, [fromIndex]):返回指定元素在數組中的最後一個的索引,若是不存在則返回 -1。從數組的後面向前查找,從 fromIndex 處開始。

let beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
beasts.indexOf('bison'); //1
beasts.indexOf('bison', 2); //4
beasts.indexOf('giraffe'); //-1

beasts.lastIndexOf('bison'); //4
beasts.lastIndexOf('bison', 2); //1
beasts.lastIndexOf('giraffe'); //-1

 

find():返回數組中知足提供的測試函數的第一個元素的值。不然返回undefined。

findIndex():返回數組中知足提供的測試函數的第一個元素的索引。不然返回-1。

let array1 = [5, 12, 8, 130, 44];
let val = array1.find(element => element > 10);
let index = array1.findIndex(element => element > 10);
val; //12
index; //1

 

forEach():對數組的每一個元素執行一次提供的函數。

filter():建立一個新數組, 其包含經過所提供函數實現的測試的全部元素。

map():建立一個新數組,其結果是該數組中的每一個元素都調用一個提供的函數後返回的結果。

reduce(reducer[,initialValue]):對數組中的每一個元素執行一個由您提供的reducer函數(升序執行),將其結果彙總爲單個返回值。reducer 函數接收4個參數:Accumulator (acc) (累計器)、Current Value (cur) (當前值)、Current Index (idx) (當前索引)、Source Array (src) (源數組)。initialValue做爲第一次調用 callback函數時的第一個參數的值。若是沒有提供初始值,則將使用數組中的第一個元素。

accumulator爲累加器,currentValue爲當前值,index爲當前索引,array爲源數組。

every():測試一個數組內的全部元素是否都能經過某個指定函數的測試。它返回一個布爾值。

some():測試數組中是否是至少有1個元素經過了被提供的函數測試。它返回的是一個Boolean類型的值。

 

let array1 = ['a', 'b', 'c'];
array1.forEach(element => console.log(element)); //'a'\n 'b'\n 'c' 
array1; //['a', 'b', 'c']

let words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
let result = words.filter(word => word.length > 6);
result; //["exuberant", "destruction", "present"]
words; //["spray", "limit", "elite", "exuberant", "destruction", "present"]

let array2 = [1, 4, 9, 16];
let map1 = array2.map(x => x * 2);
map1; //[2, 8, 18, 32]
array2; //[1, 4, 9, 16]

let array3 = [1, 2, 3, 4];
let reducer = (accumulator, currentValue) => accumulator + currentValue;
array3.reduce(reducer); //10
array3.reduce(reducer, 5); //15
array3; //1, 2, 3, 4]

let isBelowThreshold = (currentValue) => currentValue < 40;
let array4 = [1, 30, 39, 29, 10, 13];
let array5 = [1, 30, 39, 291, 10, 13];
array4.every(isBelowThreshold); // true
array5.every(isBelowThreshold); // false

let array = [1, 2, 3, 4, 5];
let even = (element) => element % 2 === 0;
array.some(even); // true

  

flat([depth]):會按照一個可指定的深度遞歸遍歷數組,並將全部元素與遍歷到的子數組中的元素合併爲一個新數組返回。depth指定要提取嵌套數組的結構深度,默認值爲 1。可用做扁平化嵌套數組。

let arr1 = [1, 2, [3, 4],[5,[6,[7]]]];
let b = arr1.flat(); 
let c = arr1.flat(2); 
let d = arr1.flat(Infinity); 
arr1; // [1, 2, [3, 4],[5,[6,[7]]]]
b; // [1, 2, [3, 4],[5,[6,[7]]]]
c; //[1,2,3,4,5,[6,[7]]]
d; //[1,2,3,4,5,6,7]

 

 

 

 

Array.from():從一個相似數組或可迭代對象建立一個新的,淺拷貝的數組實例。

Array.of():用於將一組值,轉換爲數組。

let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']

Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1

 

Array.isArray():肯定傳遞的值是不是一個array。

Array.isArray([1, 2, 3]);  // true
Array.isArray({foo: 123}); // false
Array.isArray("foobar");   // false
Array.isArray(undefined);  // false

 

先介紹這麼多,有空的時候再補充。

相關文章
相關標籤/搜索