簡體   English   中英

JavaScript 數組從索引中刪除一個元素

[英]JavaScript array delete an element from an index

大家好,我有以下數組

const options = [
    'option one', 
    'option two', 
    'some other random string', 
    'another random string', 
    'option last one', 
]

如果我想從這個數組的中間刪除一些也會更新索引的東西,我嘗試delete arr[ind]但這不會更新索引。 想從那個數組中徹底刪除。

問候

您可以嘗試通過索引過濾掉它。

options.filter((item, index) => index !== 3);

或按名稱:

options.filter((item) => item !== 'option one');

你可以使用 splice 作為

options.splice(index,1)

如果您想使用 filter 方法,您應該存儲結果,因為 filter 方法不會改變調用它的數組,而是返回一個新數組。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM