优雅的方法来删除对象中每个关键字的实例? [英] Elegant way to remove every instance of a key in an object?
本文介绍了优雅的方法来删除对象中每个关键字的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
description
的每一个实例。这样做的优雅方法是什么? 这是对象的外观:
{
属性:{
a:{
值:foo,
描述:bar
},
b:{
value:foo,
description:bar
}
}
解决方案
使用第二个参数 JSON.parse
:
const output = JSON.parse(
JSON.stringify输入),
(key,value)=> key ===description?undefined:value
);
此函数的返回值 undefined
告诉 JSON.parse
跳过它。
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
}
}
解决方案
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.
这篇关于优雅的方法来删除对象中每个关键字的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文