想瞭解數組有哪些原生方法,控制台輸出,如圖: length:數組的實例屬性,返回或設置一個數組中的元素個數。 toString():可以把數組轉換成字元串,並返回結果。 toLocaleString():返回數組中每個元素的本地化表示形式。 let a = [1234,'hello',21]; a. ...
想瞭解數組有哪些原生方法,控制台輸出,如圖:
length:數組的實例屬性,返回或設置一個數組中的元素個數。
toString():可以把數組轉換成字元串,並返回結果。
toLocaleString():返回數組中每個元素的本地化表示形式。
let a = [1234,'hello',21]; a.length; //3 a.toString(); //'1234','hello','21' a.toLocaleString(); //'1,234','hello','21'
join([seperator]):返回一個字元串,由每個元素轉換而成的字元串使用指定的seperator拼接而成。
let a = [1234,'hello',21]; a.join('-'); //'1234-hello-21'
concat():拼接兩個或更多的數組,並返回結果。此方法不會更改現有數組,而是返回一個新數組。每個參數可以是一個值,也可以是一個數組,可以接收任意多個參數。可用作拷貝數組。
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碼進行重新排序。
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
先介紹這麼多,有空的時候再補充。