我很好奇一种基于通配符从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];
};
});
关于这一点似乎对我来说非常不优雅,如果对象的大小合理,那么过程非常密集。有没有更好的方法来执行此操作?
答案 0 :(得分:7)
这是最低要求:
function deleteFromObject(keyPart, obj){
for (var k in obj){ // Loop through the object
if(~k.indexOf(keyPart)){ // If the current key contains the string we're looking for
delete obj[k]; // Delete obj[key];
}
}
}
var myObject = {
checkbox_description_1 : 'Chatoyant',
checkbox_description_2 : 'Desultory',
random_property : 'Firefly is a great program',
checkbox_mood_1 : 'Efflorescent',
checkbox_description_3 : 'Ephemeral'
};
deleteFromObject('checkbox_description', myObject);
console.log(myObject);
// myObject is now: {random_property: "Firefly is a great program", checkbox_mood_1: "Efflorescent"};
所以这非常接近你所拥有的jQuery函数
(虽然速度快一点,但考虑到它不使用jQuery,而indexOf
代替match
)
那么~
之前indexOf
的内容是什么?
indexOf
返回一个整数值:如果找不到字符串,则返回-1
;如果找到,则返回从0
开始的索引。 (所以如果找到则总是正整数)
~
是按位NOT
,它会反转此输出。碰巧的是,indexOf
的反转输出正是我们需要表示“找到”或“未找到”。
~-1
变为0
,这是一个虚假的值
~x
x
为0
或其后的-(x+1)
变为~string.indexOf('needle')
,为真值。
这样,string.contains('needle')
的行为就像!!
,这是我们在JavaScript中没有的功能。
此外,您可以在~
前面添加一个双布尔值(~string.indexOf('needle')
),将true-ish或false-ish输出转换为真实的真/假,但这不是JavaScript中必不可少的
从功能上讲,!!~string.indexOf('needle')
和~k.indexOf(keyPart)
是相同的。
如果您特别需要使用针的开始键,请替换:
k.indexOf(keyPart) === 0
使用:
{{1}}
答案 1 :(得分:2)
您可以使用How to check if a string "StartsWith" another string?:
function deleteFromObject(keyToDelete, obj) {
var l = keyToDelete.length;
for (var key in obj)
if (key.substr(0, l) == keyToDelete) // key begins with the keyToDelete
delete obj[key];
}
答案 2 :(得分:0)
var myObject = {
checkbox_description_1 : 'Chatoyant',
checkbox_description_2 : 'Desultory',
random_property : 'Firefly is a great program',
checkbox_mood_1 : 'Efflorescent',
checkbox_description_3 : 'Ephemeral'
};
const removeProperty = dyProps => ({ [dyProps]: _, ...rest }) => rest;
const remove_random_property = removeProperty('random_property');
console.log(remove_random_property(myObject));
答案 3 :(得分:0)
如果您正在寻找不改变原始对象的解决方案,您可以尝试这样的方法
const omit = (source = {}, omitKeys = []) => (
Object.keys(source).reduce((output, key) => (
omitKeys.includes(key) ? output : {...output, [key]: source[key]}
), {})
)
测试
const original = {a:1, b:2, c:3, d:4, e:5}
console.log('original: ', JSON.stringify(original));
const modified = omit(original, ['b', 'd'])
console.log('modified: ', JSON.stringify(modified));
console.log('original: ', JSON.stringify(original));
// Will log:
// original: {"a":1,"b":2,"c":3,"d":4,"e":5}
// modified: {"a":1,"c":3,"e":5}
// original: {"a":1,"b":2,"c":3,"d":4,"e":5}
这将创建一个新对象并将源对象的所有属性传播到其中,除了那些包含在例外列表 (omitKeys) 中的属性。