TLDR: How to use destructuring to speed up updating parts of one object based on another object of the same interface?
I would like to use the new ECMA2015 - 2017 JavaScript to refactor my code.
For simplicity let's say I have an object, looking like this:
export interface myObj {
name: string;
id: number;
description: string;
someBool: boolean;
anotherBool: boolean;
}
(Export interface works, because I'm working in Angular using Typescript).
Here is the old es5 function:
updateMyObj = function(oldObj, newObj) {
var someBoolSaved = oldObj.someBool;
var anotherBoolSaved = oldObj.anotherBool;
oldObj = newObj;
oldObj.someBool = someBoolSaved;
oldObj.anotherBool = anotherBoolSaved;
}
As you can see the function should update some parts of the oldObj, but also keep some parts.
Here is my attempt at refactoring the code:
updateObj(oldObj, newObj) {
let {someBool, anotherBool} = oldObj;
oldObj = newObj;
// Here is the part where I don't know how to proceed.
}
But I don't know, how I can now assign the oldObj's bools to the saved bools.
An inelegant way would be
oldObj.someBool = someBool;
oldObj.anotherBool = anotherBool;
which in this example would be fine. But in my actual task, this costs many lines of code which is why I want to refactor.
I just can't figure out the right syntax.
My attempts at coding this look similar like this:
oldObj = {someBool, anotherBool}
but this doesn't work.