简体   繁体   中英

Finding object key and deleting it

I'm trying to find a specific object key and delete it, but I cannot find a way of doing it.

Example:

{
    Tree: {
        apples: "green",
        color: "brown"
    },

    House: {
        windows: "transparent",
        flowers: "Bupleurum"
    }
}

I would like to find the House object and delete it, so I will be left only with the Tree object.

{
      Tree: {
         apples: "green",
         color: "brown"
        },
}

I tried the following function from other stack overflow question but it didn't work

function filterObject(obj, key) {
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            filterObject(obj[i], key);
        } else if (i == key) {
            delete key;
        }
    }
    return obj;
}

I'm new to JavaScript and any help will be appreciated!


UPDATE

I managed to do what I wanted with the examples you gave me, thank you! What I will also like to be able to do, is to delete multiple object keys.

Example

 var myHouse =  {
        House: {
            windows: "transparent",
            doorColor: "red",

             Kitchen: {
                 tableColor: 'silver',
                 forks: "silver"
              },

             Garden: {
                 flowers: "Bupleurum"
               }
        }
    }

I want to delete from House object, the Kitchen object and the Garden object, so later I will be only left with the House object.

var myHouse = {
            House: {
                windows: "transparent",
                doorColor: "red",
            }
        }

I tried to do it in the following way using lodash utility library, but it didn't work.

_.unset(myHouse, ['House.Kitchen', 'House.Garden']); 

From what I saw in the documentation, is possible to pass path of an array into the unset function. https://lodash.com/docs/4.17.4#unset

If the property you want to delete is not nested in another one, you can do just

function filterObject(obj, key) {
    if (obj.hasOwnProperty(key)) {
        delete obj[key];
    }
    return obj;
}

or even without the check - JavaScript won't complain if it can't find a property to delete.

Keep it simple

delete ExampleObject['House']

or

delete ExampleObject.House

Fiddle: https://jsfiddle.net/ak8xentw

Lodash provides the unset function which will also work on nested objects:

// remove Tree
_.unset(obj, 'Tree');

// Remove House.windows
_.unset(obj, 'House.windows');

UPDATE

omit can be used to remove multiple keys from a single object:

myHouse.House = _.omit(myHouse.House, ['Kitchen','Garden'])

omit doesn't change the object on which it acts so here we're setting the value of myHouse.House to the new object.

with the use of for in loop (Keep it simple) :

var ExampleObject={
    Tree: {
        apples: "green",
        color: "brown"
    },

    House: {
        windows: "transparent",
        flowers: "Bupleurum"
    }
};

for (var index in ExampleObject) {
 if(index==="House"){
    delete ExampleObject[index];
    }
 }

console.log(ExampleObject);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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