简体   繁体   English

删除关联数组中的元素

[英]Deleting elements in an associative array

How would i go about creating a set of buttons which when clicked deletes individual elements stored in an associative array? 我将如何创建一组按钮,单击这些按钮会删除存储在关联数组中的单个元素? I've tried the splice element.. but it doesn't seem to work. 我已经尝试了splice元素..但是它似乎没有用。 I'm new to programming so any help would be appreciated. 我是编程的新手,所以将不胜感激。 Thanks 谢谢

bodyText = bodyText + '<input type="button" id="btnDeleteQuestion" 
value="Delete a question" onClick="questionBank.splice.(0,1) ">';

Associative arrays in JavaScript are plain old objects with key/value pairs. JavaScript中的关联数组是带有键/值对的普通旧对象。 Use the delete operator to delete a key. 使用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"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM