javascript
javascript字典中添加数组_如何在JavaScript中使用数组方法:Mutator方法
isArray()
在介紹mutator方法之前,讓我們先看看isArray()方法,以測試對象是否是數組。這是一個布爾方法,如果變量的值等于數組,則返回true。如果對象不是數組,則此方法返回false。let fish = [ "piranha", "barracuda", "koi", "eel" ];// Test if fish variable is an arrayArray.isArray(fish);輸出:
trueisArray()方法很有用,因為通常用于測試的typeof運算符在與數組一起使用時返回object,有時需要知道對象和Array對象之間的區別。
注意,isArray()的寫法與大多數數組方法不同,數組變量作為方法的參數提供。
現在,我們知道了如何檢查以確保對象是一個數組,接下來介紹mutator方法。
pop()
我們將看到的第一個mutator方法是pop()方法,該方法刪除數組末尾的最后一個元素。
我們先從fish數組開始。
let fish = [ "piranha", "barracuda", "koi", "eel" ];為了刪除最后一項,讓我們初始化pop()方法。在本例中,它將是字符串文字“eel”。
// Use pop method to remove an item from the end of an arrayfish.pop();我們將調用數組以確保返回的數組沒有最后一項:
fish;輸出:
[ 'piranha', 'barracuda', 'koi' ]我們已經成功地從fish數組中移除“eel”。pop()方法不接受其他參數。
shift()
另一個mutator方法,shift()方法從數組的開頭刪除第一個元素。
let fish = [ "piranha", "barracuda", "koi", "eel" ];我們將使用shift()從索引0中刪除"piranha",并將所有其他元素下移一個索引號。
// Use shift method to remove an item from the beginning of an arrayfish.shift();fish;輸出:
[ 'barracuda', 'koi', 'eel' ]在這個例子中,"piranha"被刪除了,每個項目都向下移動了一個索引號。因此,通常最好盡可能使用pop()方法,因為其他數組元素將保持它們的索引位置。
push()
mutator方法push()向數組的末尾添加一個或多個新元素。
let fish = [ "piranha", "barracuda", "koi", "eel" ];為了在末尾添加一個項,我們將新元素作為函數的參數寫入。
// Use push method to add an item to the end of an arrayfish.push("swordfish");fish;輸出:
[ 'piranha', 'barracuda', 'koi', 'eel', 'swordfish' ]也可以向數組中添加多個新值。例如,fish.push("swordfish", "dragonfish")?將向索引4和5添加項。
unshift()
mutator數組方法unshift()將一個或多個新元素添加到數組的開頭。
let fish = [ "piranha", "barracuda", "koi", "eel" ];// Use unshift method to add an item to the beginning of an arrayfish.unshift("shark");fish;輸出:
[ 'shark', 'piranha', 'barracuda', 'koi', 'eel' ]在上面的示例中,將“shark”被添加到索引位置0,將所有其他數組元素向后移動一個。與shift()一樣,可以一次向數組中添加多個逗號分隔的項。
pop()和push()影響數組的結尾,而shift()和unshift()影響數組的開始。記住這一點的一個簡單方法是,記住shift()和unshift()將更改返回數組的所有索引號
splice()
splice()方法可以從數組中的任何位置添加或刪除項目。mutator方法splice()可以添加或刪除,也可以同時添加和刪除。
splice()接受三個參數——起始索引號、要刪除的項數和要添加的項數(可選)。
splice(index number, number of items to remove, items to add)splice(0, 0, "new")?將把字符串“new”添加到數組的開頭,而不刪除任何內容。
讓我們看下面的幾個示例,了解如何splice()添加和刪除數組中的項目。
使用splice()添加
如果我們將第二個參數(要刪除的項目)設置為0,splice()則會刪除零個項目。這樣,我們可以選擇僅添加從任何索引號開始的項目,從而使splice()比push()或unshift()更強大,后者只向數組的末尾或開頭添加項。
let fish = [ "piranha", "barracuda", "koi", "eel" ];// Splice a new item number into index position 1fish.splice(1, 0, "manta ray");fish;輸出:
[ 'piranha', 'manta ray', 'barracuda', 'koi', 'eel' ]新字符串“manta ray”已添加到數組中,從索引1開始。
使用splice()刪除
如果我們將第三個參數(要添加的項)留空,我們可以簡單地從數組中的任意點刪除一個項。
let fish = [ "piranha", "barracuda", "koi", "eel" ];// Remove two items, starting at index position 1fish.splice(1, 2);fish;輸出:
[ 'piranha', 'eel' ]我們從數組中刪除了兩項,從索引1,“barracuda”開始。如果刪除第二個參數,則刪除數組末尾的所有項。
使用splice()添加和刪除
一次使用所有參數,我們就可以同時在數組中添加和刪除項目。
為了演示這一點,讓我們刪除與上面相同的項,并在它們的位置上添加一個新項。
let fish = [ "piranha", "barracuda", "koi", "eel" ];// Remove two items and add onefish.splice(1, 2, "manta ray");fish;輸出:
[ 'piranha', 'manta ray', 'eel' ]splice()是修改數組任何部分的強大方法。注意,不要將splice()與slice()混淆,后者是一個訪問器數組,它將復制數組的一部分。
reverse()
reverse()方法反轉數組中元素的順序。
let fish = [ "piranha", "barracuda", "koi", "eel" ];使用reverse(),最后一個元素將是第一個元素,第一個元素將是最后一個元素。
// Reverse the fish arrayfish.reverse();fish;輸出:
[ 'eel', 'koi', 'barracuda', 'piranha' ]reverse()數組方法沒有參數。
fill()
fill()方法用靜態值替換數組中的所有元素。
let fish = [ "piranha", "barracuda", "koi", "eel" ];在fish數組中,我們有四個項目。我們應用fill()。
// Replace all values in the array with "shark"fish.fill("shark");fish;輸出:
[ 'shark', 'shark', 'shark', 'shark' ]數組中的所有四項都被替換為相同的值“shark”。fill()還接受起點和終點的可選參數。
fish.fill("shark", 1) // > [ 'piranha', 'shark', 'shark', 'shark' ]fish.fill("shark", 1, 3); // > [ 'piranha', 'shark', 'shark', 'eel' ]使用fill()我們可以用靜態值替換數組中的一個或多個元素。
sort()
sort()方法根據元素中的第一個字符對數組中的元素進行排序。在第一個字符相同的情況下,它將繼續向下并比較第二個字符,以此類推。
默認情況下,sort()將按字母順序排列的字符串數組全部為大寫或小寫。
let fish = [ "piranha", "barracuda", "koi", "eel" ];// Sort items in arrayfish.sort();fish;輸出:
[ 'barracuda', 'eel', 'koi', 'piranha' ]由于sort()基于第一個unicode字符,因此它將對大寫的項目進行排序,然后再對小寫進行排序。
讓我們修改原始數組,以使我們的字符串之一以大寫字母開頭。
let fish = [ "piranha", "barracuda", "Koi", "eel" ];fish.sort();fish;輸出:
[ 'Koi', 'barracuda', 'eel', 'piranha' ]數字位于大寫和小寫字符之前。
我們可以再次修改數組以在一個字符串項中包含一個數字。
let fish = [ "piranha", "barracuda", "Koi", "1 eel" ];fish.sort();輸出:
[ '1 eel', 'Koi', 'barracuda', 'piranha' ]sort()默認情況下不會按大小對數字數組進行排序。相反,它將只檢查數字中的第一個字符。
let numbers = [ 42, 23, 16, 15, 4, 8 ];numbers.sort();輸出:
[ 15, 16, 23, 4, 42, 8 ]為了正確地對數字進行排序,您可以創建一個比較函數作為參數。
// Function to sort numbers by sizeconst sortNumerically = (a, b) => { return a - b;}numbers.sort(sortNumerically);輸出:
[ 4, 8, 15, 16, 23, 42 ]sortNumerically比較函數允許我們按照預期進行排序。sort()將把更改應用到原始數組。
結論
在本教程中,我們回顧了javascript中的主要mutator數組方法。mutator方法修改它們使用的原始數組,而不是創建類似于copy的訪問器方法。我們學習了如何在數組的開頭或結尾添加和刪除元素,以及排序、反轉和替換數組項的值。
本文完~
總結
以上是生活随笔為你收集整理的javascript字典中添加数组_如何在JavaScript中使用数组方法:Mutator方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python多线程并行编程_Python
- 下一篇: java获取eureka_Spring