删除对象中键的每个实例的优雅方法?

Sha*_*adi 0 javascript ecmascript-6

我有一个对象,我想复制该对象并description从中删除每个实例。这样做的优雅方法是什么?该对象的外观如下:

{
  properties: {
    a: {
      value: foo,
      description: bar
    },
    b: {
      value: foo,
      description: bar
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

使用第二个参数JSON.parse

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

undefined该函数的返回值告诉我们JSON.parse跳过它。