Last active
December 21, 2017 15:47
-
-
Save dfroger/eadd93c955ae1d7a26297a84e63cb12c to your computer and use it in GitHub Desktop.
dict comprehension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const foo = { | |
x: {a: 10, b: 1}, | |
y: {a: 20, b: 2}, | |
z: {a: 30, b: 3} | |
} | |
const bar = Object.entries(foo).reduce((acc, [key, value]) => ({...acc, [key]: value.a}), {}); | |
console.log(bar); // { x: 10, y: 20, z: 30 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
foo = { | |
'x': {'a': 10, 'b': 1}, | |
'y': {'a': 20, 'b': 2}, | |
'z': {'a': 30, 'b': 3} | |
} | |
bar = {key: value['a'] for key, value in foo.items()} | |
print(bar) # {'x': 10, 'y': 20, 'z': 30} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment