JavaScript ES6, how to "save" properties from an object into a new one, without repeating the keys?

Viewed 27

I'm trying to create a new object savedStyle, with key/values from the style object, without repeating the object keys:

const style = getComputedStyle(document.body);

const savedStyle = {
  position: style.position, // the repetition
};

Why? I think it's not ideal, a small typo like psition could lead to a broken code:

Object.assign(document.body, savedStyle);

Is there any way to save multiple values, with the same key of the original object?

EDIT: verbose way:

const { position } = style;
const savedStyle = { 
  position,
};
0 Answers
Related