亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于調整/移動對象上的鍵值對的快速幫助

關于調整/移動對象上的鍵值對的快速幫助

開滿天機 2023-02-24 15:48:50
假設我有一個對象let data = { info_0: 'abc', info_1: 'def', info_2: 'ghi' }我從用戶輸入中收到了 1 的值。我需要..從對象中搜索并刪除 info_1將 info_2 移動/重命名為 info_1應該是動態的你明白了,你從用戶那里收到了索引。從對象中刪除該鍵值對并調整/移動其中的每個鍵,使其看起來像一個從 0 到 X 的數組。(在本例中為 info_0 到 info_x)我有一個很長的 for, if's & obj keys 循環解決方案。但我正在嘗試學習一些捷徑。非常感謝您的幫助編輯: 對象還可能包含各種鍵的組合,例如:client_x、charges_x、description_x 等{ info_0  : '', info_1  : '', client_0  : '', client_1  : '', client_2  : '', descpription_0 : '' }
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

您可以將屬性的值移動 1


let data = {

    info_0: 'abc',

    info_1: 'def',

    info_2: 'ghi',

    info_3: 'jkl',

    info_4: 'mno',

    info_5: 'pqr'

  },

  input = document.querySelector("#index"),

  button = document.querySelector("#remove"),

  output = document.querySelector("#output");


button.addEventListener("click", () => removeItem(+input.value));

refreshOutput();


function removeItem(index) {

  if (isNaN(index) || index < 0) return;

  index = Math.floor(index);

  while (data["info_" + index] !== undefined) {

    data["info_" + index] = data["info_" + ++index];

  }

  refreshOutput();

}


function refreshOutput() {

  output.textContent = JSON.stringify(data);

}

<input id="index" type="number" placeholder="index" />

<button id="remove">Remove</button>

<div id="output"></div>

您也可以使用delete運算符(效率很低)


let data = {

    info_0: 'abc',

    info_1: 'def',

    info_2: 'ghi',

    info_3: 'jkl',

    info_4: 'mno',

    info_5: 'pqr'

  },

  input = document.querySelector("#index"),

  button = document.querySelector("#remove"),

  output = document.querySelector("#output");


button.addEventListener("click", () => removeItem(+input.value));

refreshOutput();


function removeItem(index) {

  if (isNaN(index) || index < 0) return;

  index = Math.floor(index);

  while (("info_" + index) in data) {

    data["info_" + index] = data["info_" + ++index];

  }

  delete data["info_" + index];

  refreshOutput();

}


function refreshOutput() {

  output.textContent = JSON.stringify(data);

}

<input id="index" type="number" placeholder="index" />

<button id="remove">Remove</button>

<div id="output"></div>

或者你可以只使用一個數組和Array#splice


let data = ['abc', 'def', 'ghi', 'jkl', 'mno', 'pqr'],

  input = document.querySelector("#index"),

  button = document.querySelector("#remove"),

  output = document.querySelector("#output");


button.addEventListener("click", () => removeItem(+input.value));

refreshOutput();


function removeItem(index) {

  if (isNaN(index) || index < 0) return;

  data.splice(Math.floor(index), 1);

  refreshOutput();

}


function refreshOutput() {

  output.textContent = JSON.stringify(data);

}

<input id="index" type="number" placeholder="index" />

<button id="remove">Remove</button>

<div id="output"></div>


查看完整回答
反對 回復 2023-02-24
  • 1 回答
  • 0 關注
  • 128 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號