Elegant way to remove every instance of a key in a

2019-08-19 03:07发布

问题:

I have an object and I want to copy this object and remove every instance of description from it. What is an elegant way of doing this? here's how the object looks:

{
  properties: {
    a: {
      value: foo,
      description: bar
    },
    b: {
      value: foo,
      description: bar
    }
}

回答1:

Use the second argument to JSON.parse:

const output = JSON.parse(
  JSON.stringify(input), 
  (key, value) => key === "description" ? undefined : value
);

A return value of undefined from this function tells JSON.parse to skip it.