删除关联数组中的元素

问题描述 投票:1回答:1

我如何创建一组按钮,当单击时删除存储在关联数组中的各个元素?我已经尝试了拼接元素..但它似乎不起作用。我是编程的新手,所以任何帮助都会受到赞赏。谢谢

bodyText = bodyText + '<input type="button" id="btnDeleteQuestion" 
value="Delete a question" onClick="questionBank.splice.(0,1) ">';
javascript arrays
1个回答
2
投票

JavaScript中的关联数组是具有键/值对的普通旧对象。使用delete运算符删除密钥。

var myObject = {
  key1:"key1Value",
  key2:"key2Value",
  key3:"key3Value",  
  key4:"key4Value"  
};

console.log(myObject);

document.getElementById("btnDeleteQuestion").addEventListener("click", function(){
  delete myObject["key4"];
  console.log(myObject);  
});
<input type="button" id="btnDeleteQuestion" value="Delete a question">
© www.soinside.com 2019 - 2024. All rights reserved.