導航:首頁 > 編程語言 > js刪除數組屬性值

js刪除數組屬性值

發布時間:2024-03-28 01:56:10

js常用刪除數組方法

下面三種都會影響原數組,最後一項不影響原數組:
opop()
oshift()
osplice()
oslice()

1、pop()
pop() 方法用於刪除數組的最後一項,同時減少數組的length 值,返回被刪除的項

let colors = ["red", "green"]
let item = colors.pop(); // 取得最後一項
console.log(item) // green
console.log(colors.length) // 1

2、shift()
shift()方法用於刪除數組的第一項,同時減少數組的length 值,返回被刪除的項

let colors = ["red", "green"]
let item = colors.shift(); // 取得第一項
console.log(item) // red
console.log(colors.length) // 1

3、splice()
傳入兩個參數,分別是開始位置,刪除元素的數量,返回包含刪除元素的數組

let colors = ["red", "green", "blue"];
let removed = colors.splice(0,1); // 刪除第一項
console.log(colors); // green,blue
console.log(removed); // red,只有一個元素的數組

4、slice()
slice() 用於創建一個包含原有數組中一個或多個元素的新數組,不會影響原始數組

let colors = ["red", "green", "blue", "yellow", "purple"];
let colors2 = colors.slice(1);
let colors3 = colors.slice(1, 4);
console.log(colors) // red,green,blue,yellow,purple
concole.log(colors2); // green,blue,yellow,purple
concole.log(colors3); // green,blue,yellow

② js中怎麼將數組中某個元素去掉

本節的內容,通過一個例子,教大家刪除數組中某一個元素的方法。
1,html部分

復制代碼代碼示例:
<input type="button" value="刪除數組i位置的元素" onclick="arrayRemove();"/>
2,js代碼部分

復制代碼代碼示例:
<script>
/**
* 刪除數組中某個元素
* by www.jbxue.com
*/
function arrayRemove()
{
//初始化數組
var array = new Array();
for(var i=0; i<10; i++)
{
array.push(i+"name");
}
//檢測要刪除的元素(刪除元素值為:7name)
for(var i=0; i<array.length; i++)
{
if(array[i] == "7name")
{
array = removeElement(i,array);//刪除方法
}
}
for(var i=0; i<array.length; i++)
{
alert(array[i]);
}
}
function removeElement(index,array)
{
if(index>=0 && index<array.length)
{
for(var i=index; i<array.length; i++)
{
array[i] = array[i+1];
}
array.length = array.length-1;
}
return array;
}
</script>

③ js怎麼從數組中刪除指定值(不是指定位置)的元素

var a = new Array("a","b","cc","d3");//

刪除a數組的cc元素

//jQuery.inArray()函數用於在數組中搜索指定的值,並返回專其索引值。如果數組中不存在屬該值,則返回 -1。該函數屬於全局jQuery對象。

jquery 1.2中添加的該靜態方法var index = $.inArray("cc",a);
if(index>=0){//arrayObject.splice(index,howmany,item1,.....,itemX)
//參數描述//index 必需。

整數,規定添加/刪除項目的位置,使用負數可從數組結尾處規定位置,//howmany 必需。要刪除的項目數量。如果設置為 0,則不會刪除項目。

//item1, ..., itemX 可選。向數組添加的新項目。
a.splice(index,1);
alert(a.totring());
}else{
alert("error"); return false;
}

④ JS刪除數組中元素

1、splice

splice(index,len,[item]) 注釋:該方法會改變原始數組。

splice有3個參數,它也可以用來替換/刪除/添加數組內某一個或者幾個值

index:數組開始下標 len: 替換/刪除的長度 item:替換的值,刪除操作的話 item為空

如:arr = ['a','b','c','d']

刪除 ---- item不設置

arr.splice(1,1) //['a','c','d'] 刪除起始下標為1,長度為1的一個值,len設置的1,如果為0,則數組不變

arr.splice(1,2) //['a','d'] 刪除起始下標為1,長度為2的一個值,len設置的2

替換 ---- item為替換的值

arr.splice(1,1,'ttt') //['a','ttt','c','d'] 替換起始下標為1,長度為1的一個值為『ttt',len設置的1

arr.splice(1,2,'ttt') //['a','ttt','d'] 替換起始下標為1,長度為2的兩個值為『ttt',len設置的1

添加 ---- len設置為0,item為添加的值

arr.splice(1,0,'ttt') //['a','ttt','b','c','d'] 表示在下標為1處添加一項『ttt'

看來還是splice最方便啦

2、delete

delete刪除掉數組中的元素後,會把該下標出的值置為undefined,數組的長度不會變

如:delete arr[1] //['a', ,'c','d'] 中間出現兩個逗號,數組長度不變,有一項為undefined

⑤ js中刪除數組元素的幾種方法

刪除步驟如下:

ar arr = [ 1, 2, 3, 4, 5 ];

//原始數組

alert("原始數組:" + arr);// 1,2,3,4,5

//刪除並且返回第一個元素

註:重復以上步驟即內可

一、JavaScript

⑥ js中刪除數組或對象

在vue中使用

vue.delete()

刪除對象屬性

通過delete操作符, 可以實現對對象屬性的刪除操作, 返回值是布爾

例:      var obj={name: 'zhagnsan',age: 19 }

            delete obj.name //true

            typeof obj.name //undefined

同樣可用於函數,數組,變數,對象,但對象不能刪除,只能做到刪除對象屬性

刪除變數

例: var name ='zs' //已聲明的變數

        delete name  //false

        console.log(typeof name)  //String

        age = 19  //未聲明的變數

        delete age //true

        typeof age //undefined

        this.val = 'fds'  //window下的變數

        delete this.val   //true

        console.log(typeof this.val)  //undefined

刪除數組

以聲明數組返回false,未聲明返回true

var arr = ['1','2','3'] ///已聲明的數組

delete arr //false

console.log(typeof arr)  //object

arr = ['1','2','3']  //未聲明的數組

delete arr  //true

console.log(typeof arr)  //undefined

var arr = ['1','2','3']  //已聲明的數組

delete arr[1]  //true

console.log(arr)  //['1','empty','3']

刪除函數

var fn = function(){} //已聲明的函數

delete fn //false

console.log(typeof fn)  //function

fn = function(){}  //未聲明的函數

delete fn //true

console.log(typeof fn)  //undefined

刪除對象

var person = {

  height: 180,

  long: 180,

  weight: 180,

  hobby: {

    ball: 'good',

    music: 'nice'

  }

}

delete person  ///false

console.log(typeof person)  //object

var person = {

  height: 180,

  long: 180,

  weight: 180,

  hobby: {

    ball: 'good',

    music: 'nice'

  }

}

delete person.hobby  ///true

console.log(typeof person.hobby)  //undefined

閱讀全文

與js刪除數組屬性值相關的資料

熱點內容
刀劍神域txt全卷 瀏覽:246
給一個看電影不卡的網站 瀏覽:61
衛星圖在什麼網站找 瀏覽:939
編程模擬器666制葯廠那關怎麼過 瀏覽:281
斑果app怎麼下載 瀏覽:221
win10系統怎麼不能備份 瀏覽:552
手機桌面刪除應用程序圖標不見了 瀏覽:572
韓國19禁百度雲電影排行榜 瀏覽:794
超甜超欲的日本電影有哪些 瀏覽:566
女的有性癮和小伙在船上偷情電影 瀏覽:914
unwrapuvw教程 瀏覽:13
java隱藏手機號中間四位 瀏覽:898
全員超市喪屍 瀏覽:256
國外文獻網站怎麼看 瀏覽:664
在線電影 0855 瀏覽:497
大數據反腐有些什麼步驟 瀏覽:611
農村小孩電影 瀏覽:224
電影動漫大全 瀏覽:115
韓國推理片電影在線收看 瀏覽:733

友情鏈接