Skip to content

web-2017/remove-any-key-in-object

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 

Repository files navigation

How remove nasted deep key in object

Array example

const obj = [
  {
    name: "dsad",
    created: "afads",
    updated: "asdf",
    desc: {
      name: "desc",
      created: "afads",
      updated: "asdf",
      newArr: [
        {
          name: "desc",
          created: "afads",
          updated: "asdf"
        }
      ]
    }
  },
  {
    name: "2222",
    created: "afads",
    updated: "asdf"
  },
  {
    name: "333",
    created: "afads",
    updated: "asdf"
  }
];

Function

const removeKeys = (props) => {
  getObj(props);

  function getObj(props) {
    for (const key in props) {
      if (typeof props === "object") {
        getObj(props[key]);

        ["data", "id"].forEach((item) => {
          if (props[key][item]) delete props[key][item];
        });

        return props[key];
      }
    }
  }
  return props;
};


const newObj = removeKeys(obj);

console.log(obj);

Releases

No releases published

Packages

No packages published