It overwrites dest with properties and values of (however many) source objects, then returns dest.
The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.
Vivaldi, Chrome, Opera, and Firefox in up to date releases know this feature also, but Mirosoft don't until today, neither in Internet Explorer nor in Edge.
Since its a question of the past but the problem of present. Would suggest one more solution: Just pass the key and values to the function and you will get a map object.
var map = {};
function addValueToMap(key, value) {
map[key] = map[key] || [];
map[key].push(value);
}
Year 2017 answer:
Object.assign()
Object.assign(dest, src1, src2, ...) merges objects.
It overwrites
dest
with properties and values of (however many) source objects, then returnsdest
.Live example
Year 2018 answer: object spread operator
{...}
From MDN:
Live example
It works in current Chrome and current Firefox. They say it doesn’t work in current Edge.
Year 2019 answer
Object assignment operator+=
:Oops... I got carried away. Smuggling information from the future is illegal. Duly obscured!
A short and elegant way in next Javascript specification (candidate stage 3) is:
obj = { ... obj, ... { key3 : value3 } }
A deeper discussion can be found in Object spread vs Object.assign and on Dr. Axel Rauschmayers site.
It works already in node.js since release 8.6.0.
Vivaldi, Chrome, Opera, and Firefox in up to date releases know this feature also, but Mirosoft don't until today, neither in Internet Explorer nor in Edge.
Since its a question of the past but the problem of present. Would suggest one more solution: Just pass the key and values to the function and you will get a map object.