const foo = { ONE: 1, TWO: 2 }; const mapMultiplier = (params, multiplier) => { // Only shallow clone works, deep clone is expensive and need other mechanisms const paramsCopy = { ...params }; // Object.freeze(params); // Object.assign({}, params) paramsCopy.ONE = paramsCopy.ONE * multiplier; paramsCopy.TWO = paramsCopy.TWO * multiplier; return paramsCopy; }; console.log(mapMultiplier(foo, 2)); console.log(mapMultiplier(foo, 3)); console.log(mapMultiplier(foo, 4)); // Value of foo { 'ONE': 1, 'TWO': 2 } console.log('foo: ', foo);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter