'How do I overwrite property if exists? [duplicate]

Say I have object as follows:

let newObject = {
 foo: 'foo-value',
 bar: 'bar-value', 
 baz: 'baz-value'
};

I have also a list:

let list = ['foo', 'baz']

I want to overwrite 'foo-value' and 'baz-value' (value of keys that appear in the list) with some 'default-value'. So I have:

{
 foo: 'default-value',
 bar: 'bar-value', 
 baz: 'default-value'
};

If I have a different object, let's say

let differentObject = {
 x: 'x-value',
 y: 'y-value'
}

I don't want to add any default value so spread operator didn't work for me.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source