Skip to content

Change JS Object map to use reduce #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amrdraz
Copy link

@amrdraz amrdraz commented Sep 23, 2018

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

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

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

```j
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant