Skip to content

Instantly share code, notes, and snippets.

@dfroger
Last active December 21, 2017 15:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfroger/eadd93c955ae1d7a26297a84e63cb12c to your computer and use it in GitHub Desktop.
Save dfroger/eadd93c955ae1d7a26297a84e63cb12c to your computer and use it in GitHub Desktop.
dict comprehension
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 }
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