Skip to content

Instantly share code, notes, and snippets.

@kutyel
Last active August 2, 2019 09:53
Show Gist options
  • Save kutyel/f5f188ce15a0b11155bc121e62f40b7c to your computer and use it in GitHub Desktop.
Save kutyel/f5f188ce15a0b11155bc121e62f40b7c to your computer and use it in GitHub Desktop.
Object.prototype.fromEntries()
// Supongamos, como ejemplo, que queremos elevar al cubo (^3) las habilidades de Eren
const eren = { strength: 50, health: 100, height: 170 }
// Utilizamos nuestro ya conocido método `Object.entries`
// para pasar del dominio de los objetos al de los arrays...
const aux = Object.entries(eren)
// > [["strength", 50], ["health", 100], ["height", 170]]
// Ahora que estamos en el dominio de los arrays, podemos hacer un simple `map`!
const array = aux.map(([key, value]) => [key, value ** 3])
// > [["strength", 125000], ["health", 1000000], ["height", 4913000]]
// Hemos conseguido lo que queríamos, transformar los valores,
// pero ahora tenemos un array de arrays...
const titanEren = Object.fromEntries(array) // ...para eso tenemos el nuevo método!!
// > { strength: 125000, health: 1000000, height: 4913000 } 🚀🚀🚀
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment