第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

刪除JavaScript中的數(shù)組元素-刪除VS拼接

刪除JavaScript中的數(shù)組元素-刪除VS拼接

慕桂英3389331 2019-05-31 16:47:03
刪除JavaScript中的數(shù)組元素-刪除VS拼接使用這個(gè)delete操作者在數(shù)組元素上,而不是使用這個(gè)Array.splice方法?例如:myArray = ['a', 'b', 'c', 'd'];delete myArray[1];//  ormyArray.splice (1, 1);如果我可以像刪除對(duì)象一樣刪除數(shù)組元素,那么為什么還要使用Splice方法呢?
查看完整描述

4 回答

?
心有法竹

TA貢獻(xiàn)1866條經(jīng)驗(yàn) 獲得超5個(gè)贊

delete將刪除對(duì)象屬性,但不會(huì)重新索引數(shù)組或更新其長(zhǎng)度。這使它看起來(lái)似乎是未定義的:

> myArray = ['a', 'b', 'c', 'd']
  ["a", "b", "c", "d"]> delete myArray[0]
  true> myArray[0]
  undefined

注意,它實(shí)際上沒(méi)有設(shè)置為undefined,而是將屬性從數(shù)組中移除,使其成為出現(xiàn)沒(méi)有定義。Chrome dev工具通過(guò)打印明確了這一區(qū)別。empty記錄數(shù)組時(shí)。

> myArray[0]
  undefined> myArray  [empty, "b", "c", "d"]

myArray.splice(start, deleteCount)實(shí)際上移除元素,重新索引數(shù)組,并更改其長(zhǎng)度。

> myArray = ['a', 'b', 'c', 'd']
  ["a", "b", "c", "d"]> myArray.splice(0, 2)
  ["a", "b"]> myArray  ["c", "d"]


查看完整回答
反對(duì) 回復(fù) 2019-05-31
?
肥皂起泡泡

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超6個(gè)贊

Array.emove()方法

約翰·雷西格,jQuery的創(chuàng)建者創(chuàng)建了一個(gè)非常方便的Array.remove方法,以便在項(xiàng)目中始終使用它。

// Array Remove - By John Resig (MIT Licensed)Array.prototype.remove = function(from, to) {
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);};

下面是一些如何使用它的例子:

// Remove the second item from the arrayarray.remove(1);// Remove the second-to-last item from the arrayarray.remove(-2);
// Remove the second and third items from the arrayarray.remove(1,2);
// Remove the last and second-to-last items from the arrayarray.remove(-2,-1);

約翰網(wǎng)站


查看完整回答
反對(duì) 回復(fù) 2019-05-31
?
元芳怎么了

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊

因?yàn)镈ELETE只從數(shù)組中的元素中刪除對(duì)象,所以數(shù)組的長(zhǎng)度不會(huì)改變。Splice移除對(duì)象并縮短數(shù)組。

下面的代碼將顯示“a”、“b”、“未定義”、“d”

myArray = ['a', 'b', 'c', 'd']; delete myArray[2];for (var count = 0; count < myArray.length; count++) {
    alert(myArray[count]);}

而這將顯示“a”、“b”、“d”

myArray = ['a', 'b', 'c', 'd']; myArray.splice(2,1);for (var count = 0; count < myArray.length; count++) {
    alert(myArray[count]);}


查看完整回答
反對(duì) 回復(fù) 2019-05-31
?
溫溫醬

TA貢獻(xiàn)1752條經(jīng)驗(yàn) 獲得超4個(gè)贊

我無(wú)意中發(fā)現(xiàn)了這個(gè)問(wèn)題,同時(shí)試圖理解如何從Array中刪除每個(gè)元素的出現(xiàn)。下面是一個(gè)比較splicedelete為了移除每一個(gè)'c'items陣列。

var items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'];while (items.indexOf('c') !== -1) {
  items.splice(items.indexOf('c'), 1);}console.log(items);
   // ["a", "b", "d", "a", "b", "d"]items = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'];while (items.indexOf('c') !== -1) {
  delete items[items.indexOf('c')];}console.log(items); // ["a", "b", undefined, "d", "a", "b", undefined, "d"]


查看完整回答
反對(duì) 回復(fù) 2019-05-31
  • 4 回答
  • 0 關(guān)注
  • 694 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)