Using Object Restructuring is straightforward for the black properties of an object, as in the following example:
const original = {
a: 1,
b: 2,
c: 3,
evil: "evil",
ugly: "ugly",
};
const { evil, ugly, ...sanitized } = original;
console.log(sanitized);
I wonder if there is a similar complicated way to do the same, but using a white list of properties (in the example:) { a, b, c }. Very often I have to convert a subset of the available properties as JSON, and such functionality will make the code more understandable and safe.
I found a similar question, but this is not exactly the same problem:
Is there an easier way to map the properties of one object to another in ES6 / ES2015?
Edit: It is a pity that the following code does not work, as it returns the original object, not the filtered one.
const sanitized = {a, b, c} = original;