python-vs-javascript
python-vs-javascript copied to clipboard
Change JS Object map to use reduce
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