python-vs-javascript icon indicating copy to clipboard operation
python-vs-javascript copied to clipboard

Change JS Object map to use reduce

Open amrdraz opened this issue 7 years ago • 0 comments

I feel that if we use the python dict comprehension one liner the equivalent JS one liner should be used as well

const newDict = Object.entries(originalDict).reduce(newDict, ([key, value]) => ({ ...newDict, [key]: value * value }), {})

for better performance we could use the more verbose but still true to the expression of mapping example bellow

const newDict = Object.entries(originalDict).reduce(newDict, ([key, value]) => {
 newDict[key] = value * value 
 return newDict
}, {})

in this solution I just used JS's destruct and variable key dictionary

in the proposed PR I used the combination of arrow function default return and object spread to make it a one liner

amrdraz avatar Sep 23 '18 09:09 amrdraz