简体   繁体   中英

How to remove set of keys from a nested objects?

I have a data like below, which have some unnecessary keys, and the data is deeply nested so I thought of writing a function to remove those keys recursively, But it does removing the keys only for the root level not removing entire array of objects.

Data in-hand: (stated few for better understanding)

[
  {
  "active":false,
  "name":null,
  "hidden":false,
  "created":"",
  "lastModified":"",
  "version":null,
  "icon":null,
  "path":null,
  "type":"list",
  "id":"21536422-a9ff-4fcd-a5e7-62d246f1f01e",
  "parent":"",
  "children": []
  }
]

expected data:

[
  {
  "active":false,
  "type":"list",
  "id":"21536422-a9ff-4fcd-a5e7-62d246f1f01e",
  "parent":"",
  "children": []
  }
]

keys to remove recursively:

"name", "hidden", "created", "lastModified", "version", "icon", "path"

Function:

function deleteObjKeys(state) {
    return state.map(function recursive(o ,index) {
        //every obj contains theses keys name,
      // hidden,
      // created,
      // lastModified,
      // createdBy,
      // version,
      // icon,
      // path,
      // type,   needs to remove it in that obj
        if(o.hasOwnProperty("hidden")) {
            const {hidden, createdOn, lastModified, createdBy, version, icon, path, objectCategory, objectType, title, id, propertyConfig, _id,  ...keepAttrs} =  o;
            o = {};
            o = {...keepAttrs};
        }

        if (o.children) {
            o.children.map(recursive)
          }
        return o;
    })
}

I don't know, what went wrong, You can check the working demo here , you can check the console to get an idea.

I hope, it clear and Any help on this really helpful.

Since you have a nested set up deep in the object, the logic mentioned below loops through the entire array and when it encounters children array, it uses recursion to delete similar keys in children array. This keeps going through out children objects as well.

Here is a working demo - https://stackblitz.com/edit/react-yuvxat?file=index.js

function deleteObjKeys(state) {
  if(state){
  state.forEach(function(s){
    delete s.name;
    delete s.hidden;
    delete s.created;
    delete s.lastModified;
    delete s.createdBy
    delete s.version;
    delete s.icon;
    delete s.path;
    delete s.type;
    if(s.children){
      state.children = deleteObjKeys(s.children);
    }
  })
  }
return state;
}

my solution

This is my solution.

let data = [
  {
  "active":false,
  "name":null,
  "hidden":false,
  "created":"",
  "lastModified":"",
  "version":null,
  "icon":null,
  "path":null,
  "type":"list",
  "id":"21536422-a9ff-4fcd-a5e7-62d246f1f01e",
  "parent":"",
  "children": []
  }
];

let unwatedKeys = ["name", "hidden", "created", "lastModified", "version", "icon", "path"]

  unwatedKeys.forEach(element => {
      delete data[0][element];
    });

code : https://jsfiddle.net/khq5ufn7/2/

Your script works. You just have to store the result of the "inner" .map() call in .children

Change:

o.children.map(recursive)

To:

o.children = o.children.map(recursive)

 const input = [ { "active":false, "name":null, "hidden":false, "created":"", "lastModified":"", "version":null, "icon":null, "path":null, "type":"list", "id":"21536422-a9ff-4fcd-a5e7-62d246f1f01e", "parent":"", "children": [ { "active":false, "name":null, "hidden":false, "created":"", "lastModified":"", "version":null, "icon":null, "path":null, "type":"list", "id":"21536422-a9ff-4fcd-a5e7-62d246f1f01e", "parent":"", "children": [ { "active":false, "name":null, "hidden":false, "created":"", "lastModified":"", "version":null, "icon":null, "path":null, "type":"list", "id":"21536422-a9ff-4fcd-a5e7-62d246f1f01e", "parent":"", "children": [] } ]} ]} ] function deleteObjKeys(state) { return state.map(function recursive(o, index) { if (o.hasOwnProperty("hidden")) { const {hidden, createdOn, lastModified, createdBy, version, icon, path, objectCategory, objectType, title, id, propertyConfig, _id, ...keepAttrs} = o; o = { ...keepAttrs }; } if (o.children) { o.children = o.children.map(recursive); // <-- } return o; }) } const result = deleteObjKeys(input); console.log(result);

add this function

function deleteObjKeys(data) {
  let unwatedKeys = ["name", "hidden", "created", "lastModified", "version", "icon", "path"]

  unwatedKeys.forEach(element => {
      delete data[0][element];
    });
  return data
}

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