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

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

將數(shù)組元素從一個(gè)數(shù)組位置移動(dòng)到另一個(gè)數(shù)組位置。

將數(shù)組元素從一個(gè)數(shù)組位置移動(dòng)到另一個(gè)數(shù)組位置。

慕妹3146593 2019-07-05 10:09:57
將數(shù)組元素從一個(gè)數(shù)組位置移動(dòng)到另一個(gè)數(shù)組位置。我很難弄清楚如何移動(dòng)數(shù)組元素。例如,考慮到以下情況:var arr = [ 'a', 'b', 'c', 'd', 'e'];我如何寫(xiě)一個(gè)函數(shù)來(lái)移動(dòng)'d'以前'b'?或'a'后'c'?移動(dòng)后,應(yīng)更新其余元素的索引。這意味著在移動(dòng)arr后的第一個(gè)例子中,arr[0]將=‘a(chǎn)’,arr[1]=‘d’arr[2]=‘b’,arr[3]=‘c’,arr[4]=‘e’。這看起來(lái)應(yīng)該很簡(jiǎn)單,但是我不能把我的頭繞在這上面。
查看完整描述

3 回答

?
倚天杖

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

這是我在JSPerf上找到的一條郵輪.。

Array.prototype.move = function(from, to) {
    this.splice(to, 0, this.splice(from, 1)[0]);};

讀起來(lái)很棒,但是如果您想要性能(在小數(shù)據(jù)集中),請(qǐng)嘗試.

 Array.prototype.move2 = function(pos1, pos2) {
    // local variables
    var i, tmp;
    // cast input parameters to integers
    pos1 = parseInt(pos1, 10);
    pos2 = parseInt(pos2, 10);
    // if positions are different and inside array
    if (pos1 !== pos2 && 0 <= pos1 && pos1 <= this.length && 0 <= pos2 && pos2 <= this.length) {
      // save element from position 1
      tmp = this[pos1];
      // move element down and shift other elements up
      if (pos1 < pos2) {
        for (i = pos1; i < pos2; i++) {
          this[i] = this[i + 1];
        }
      }
      // move element up and shift other elements down
      else {
        for (i = pos1; i > pos2; i--) {
          this[i] = this[i - 1];
        }
      }
      // put element from position 1 to destination
      this[pos2] = tmp;
    }
  }

我不能接受任何榮譽(yù),這一切都應(yīng)該歸功于理查德·斯卡拉特..它優(yōu)于本文中針對(duì)較小數(shù)據(jù)集的基于拼接的方法。性能試驗(yàn)..然而,在較大的數(shù)據(jù)集上,它要慢得多。正如達(dá)維恩指出的.


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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