js移除數組中指定元素【轉】


首先需要找到元素的下標,使用splice函數進行移除:

var array = ["zhangsan", "lisi", "wangwu"];
var index = array.indexOf("lisi");
if (index > -1) {
    array.splice(index, 1);
}

splice函數的第二個參數指刪除的數目。splice直接修改原數組,並把刪除的所有元素以另一個新數組的方式返回。

文章來自:https://blog.csdn.net/qq_36742720/article/details/89423150
更多:https://www.w3school.com.cn/jsref/jsref_splice.asp


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM