[英]Remove items from associative array in Javascript
I have an associative array in my JS which I want to feed my Select. 我的JS中有一个关联数组,我想将其输入。
These are the standard hours available that will refresh my availablehours (when page loads); 这些是可刷新我的可用时间(页面加载时)的可用标准时间;
var standardhours = {
"09" : '9AM',
"10" : '10AM',
"11" : '11AM',
"12" : 'Noon',
"13" : '1PM',
"14" : '2PM',
"15" : '3PM',
"16" : '4PM',
"17" : '5PM',
"18" : '6PM',
"19" : '7PM'
};
These the available hours that I want to feed my Select. 这些是我想喂我的精选的可用时间。
var availablehours = {
"09" : '9AM',
"10" : '10AM',
"11" : '11AM',
"12" : 'Noon',
"13" : '1PM',
"14" : '2PM',
"15" : '3PM',
"16" : '4PM',
"17" : '5PM',
"18" : '6PM',
"19" : '7PM'
};
This is my dynamic variable that changes based on database changes. 这是我的动态变量,它根据数据库更改而更改。 These are the hours taken by someone already.
这些是某人已经花费的时间。
takenHours = {"09", "18"};
I want to remove the takenHours from the availablehours associative array. 我想从availablehours关联数组中删除takenHours。 Not sure how.
不确定如何。 The thought process I have feels very manual and simplistic.
我的思考过程非常手动和简单。
If I understand correctly, then you should be able to achieve this kind of mutation of availablehours
via the delete
operator, as shown: 如果我理解正确,那么您应该能够通过
delete
运算符实现这种availablehours
的突变,如下所示:
var availablehours = { "09": '9AM', "10": '10AM', "11": '11AM', "12": 'Noon', "13": '1PM', "14": '2PM', "15": '3PM', "16": '4PM', "17": '5PM', "18": '6PM', "19": '7PM' }; console.log(availablehours); var takenHours = ["09", "18"]; // Iterate through the hours to be removed for (var takenHour of takenHours) { // For each hour key, use the delete operator to remove // the key/value pair from availablehours completlty delete availablehours[takenHour] } // availablehours will no longer contain "09" or "18" key/value pairs console.log(availablehours);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.