在es6中是否可以将对象分解为新对象? [英] Is there a way to destructure an object into a new object in es6?
本文介绍了在es6中是否可以将对象分解为新对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有这个对象:
const payload = {
apple: 1,
dog: 2,
cat: 3
}
,我想将其分解为仅包含apple
和dog
的新对象:
and I want to destructure it into a new object that only contains apple
and dog
:
const newPayload = {
apple:1,
dog: 2
}
类似:
const {{apple, dog} : newPayload} = payload
显然以上说法是错误的,但想知道是否有一种方法可以做到这一点.
Obviously the above is wrong, but wondering if there is a way to do something like this.
谢谢!
推荐答案
您可以使用delete
:
const newPayload = { ...payload };
delete newPayload.cat
或者:
const { cat, ...newPayload } = payload;
Lodash也有omit()
:
const newPayload = _.omit(payload, ['cat']);
这篇关于在es6中是否可以将对象分解为新对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文