Last active
August 2, 2019 09:53
-
-
Save kutyel/f5f188ce15a0b11155bc121e62f40b7c to your computer and use it in GitHub Desktop.
Object.prototype.fromEntries()
This file contains hidden or 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
// 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