对象属性动态删除 [英] Object properties dynamic delete

查看:129
本文介绍了对象属性动态删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇一种基于通配符从javascript对象动态删除属性的改进方法。首先,假设我有以下对象:

I am curious about an improved way to dynamically delete properties from a javascript object based on wildcards. Firstly, suppose I have the following object:

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'的形式删除了所有属性,并保留了对象的其余部分,如下所示:

Task

Now, the end result is to have removed all properties under the guise of 'checkbox_description' and leave the rest of the object intact as shown:

object =
{
    random_property : 'Firefly is a great program',
    checkbox_mood_1 : 'Efflorescent',
}



我的解决方案



目前我的解决方案涉及jquery和以下代码:

My solution

At present my solution involves jquery and the following code:

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];
    };
});



问题



关于这一点似乎非常对我来说是不优雅的,如果对象的尺寸合理,那么过程非常密集。有没有更好的方法来执行此操作?

Issue

Something about this seems very inelegant to me and if the object were to be of reasonable size very process intensive. Is there a better way of performing this operation?

推荐答案

这是最低要求:

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 而不是匹配

So that's pretty close to the jQuery function you have.
(Though a little faster, considering it doesn't use jQuery, and indexOf instead of match)

那么,在 indexOf 之前是什么?

So, what's with the ~ before indexOf?

indexOf 返回一个整数值: -1 if如果找到,则找不到字符串,索引从 0 开始。 (所以如果找到则总是一个正整数)

是按位 NOT ,反转这个输出。碰巧的是, indexOf 的反转输出正是我们需要指示找到或未找到。

indexOf returns a integer value: -1 if the string is not found, and a index, starting from 0, if it is found. (So always a positive integer if found)
~ is a bitwise NOT, that inverts this output. As it happens to be, the inverted output of indexOf is just what we need to indicate "found" or "not found".

〜-1 变为 0 ,这是一个假的值。

~x ,其中 x 0 或邮寄,变为 - (x + 1),一个真正的值。

~-1 becomes 0, a false-ish value.
~x, where x is 0 or postitive, becomes -(x+1), a true-ish value.

这样, ~string.indexOf('needle')的行为类似于 string.contains('needle'),这是我们在JavaScript中没有的函数。

This way, ~string.indexOf('needle') acts like string.contains('needle'), a function that we don't have in JavaScript.

此外,您可以在<$ c $前面添加一个双布尔值( !! ) c>〜,将true-ish或false-ish输出转换为真实的真/假,但这在JavaScript中不是必需的。

在功能上, ~string.indexOf('needle') !! ~string.indexOf('needle')相等。

Additionally, you could add a double boolean not (!!) in front of the ~, to convert the true-ish or false-ish output to a real true / false, but that's not necessary in JavaScript.
Functionally, ~string.indexOf('needle') and !!~string.indexOf('needle') are equal.

如果您需要使用针的开始键,请替换:

In case you specifically need the key to begin with the needle, replace the:

~k.indexOf(keyPart)

使用:

k.indexOf(keyPart) === 0

这篇关于对象属性动态删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆