開發與維運

js中的數組刪除、對象屬性刪除

刪除數組中的元素

使用splice(會改變原數組)

  arr.splice(index,len,item);
   //index,刪除或者替換的起始位置
   //len,刪除或者替換的長度
   //item,替換的內容
   arr = ["a","b","c","d"]
   //刪除
   arr.splice(1,2) ====>["a","d"]
   //替換
   arr.splice(1,1,"tt") ====> ["a","tt","c","d"]
   arr.splice(1,2,"tt") ====> ["a","tt","d"]
   //添加
   arr.splice(1,0,"ttt") ====> ["a","tt","b","c","d"]

使用remove刪除指令的元素

arr.remove(xx)

使用delete


//數組長度不變,置為undefined
    delete arr[1] ====> ["a",,"c","d"]

刪除對象的屬性

使用delete

delete obj.properti
let map = new Map(["name","xxx"])
map.delete("name")

Leave a Reply

Your email address will not be published. Required fields are marked *