我很想了解一种更好的方法,基于通配符,动态删除javascript对象中的属性。首先,假设我有以下对象:
object =
{
checkbox_description_1 : 'Chatoyant',
checkbox_description_2 : 'Desultory',
random_property : 'Firefly is a great program',
checkbox_mood_1 : 'Efflorescent',
checkbox_description_3 : 'Ephemeral'
}
任务
现在,最终目标是删除所有以“checkbox_description”为幌子的属性,并保留对象的其余部分,如下所示:
object =
{
random_property : 'Firefly is a great program',
checkbox_mood_1 : 'Efflorescent',
}
我的解决方案
目前,我的解决方案涉及到jquery和以下代码:
var strKeyToDelete = 'checkbox_description'
/* Start looping through the object */
$.each(object, function(strKey, strValue) {
/* Check if the key starts with the wildcard key to delete */
if(this.match("^"+strKey) == strKeyToDelete) {
/* Kill... */
delete object[strKey];
};
});
问题
我觉得这个操作很不优雅,如果对象的大小合理的话,会非常耗费资源。有没有更好的方法来执行这个操作?
~
是什么意思——在JavaScript中,它不是一个常见的运算符。 - Jamiec~
的作用。 - Cerbrus