admin管理员组文章数量:1316980
In JavaScript/Typescript,
What is the short version to destructure and then assign in a new object like so :
const payload: MyPayload = { a: 1, b: 2, c: 3, d: 4, e: 5 }
// Destruct
const { a, c, e } = payload;
// New Obj
const newPayload = {
a, c, e
};
In JavaScript/Typescript,
What is the short version to destructure and then assign in a new object like so :
const payload: MyPayload = { a: 1, b: 2, c: 3, d: 4, e: 5 }
// Destruct
const { a, c, e } = payload;
// New Obj
const newPayload = {
a, c, e
};
Share
Improve this question
edited Jul 12, 2019 at 7:25
Cœur
38.8k26 gold badges205 silver badges277 bronze badges
asked Mar 30, 2019 at 18:06
ScarauxScaraux
4,1726 gold badges46 silver badges85 bronze badges
0
3 Answers
Reset to default 6You could take a destructuring assignment with the object and short hand properties for a new object.
const
getParts = ({ a, c, e }) => ({ a, c, e }),
payload = { a: 1, b: 2, c: 3, d: 4, e: 5 },
parts = getParts(payload);
console.log(parts);
You can use IIFE
const payload = { a: 1, b: 2, c: 3, d: 4, e: 5 }
const obj = (({a,c,e}) => ({a,c,e}))(payload)
console.log(obj)
You can create the object during destructuring using object rest:
const payload = { a: 1, b: 2, c: 3, d: 4, e: 5 }
const { b, d, ...newPayload } = payload
console.log(newPayload)
本文标签: typescriptJavaScript Destructure and assign to new objectStack Overflow
版权声明:本文标题:typescript - JavaScript Destructure and assign to new object - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742017907a2414120.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论