Learn about the Object.entries()
and Object.fromEntries()
object method.
/**
* Object.entries()
*
* @return method returns a two-dimensional array of a given object's.
*/
/**
* Object.fromEntries()
*
* @return methods return a object given a two-dimensional array.
*/
Using entries()
:
const person = { firstName: "henry", lastName: "arbolaez" }
const personArray = Object.entries(person)
/**
* @return [["firstName", "henry"], ["lastName", "arbolaez"]]
*
*/
for(let [key, value] of personArray) {
console.log(`${key}: ${value}`);
}
/**
* @return
* firstName: henry
* lastName: arbolaez
*/
Using fromEntries()
:
/**
* If we ever want to turn that 2D array back into a object
* we could use now Object.fromEntries(2dArray)
*/
const personArray = [["firstName", "henry"], ["lastName", "arbolaez"]];
Object.fromEntries(personArray);
/**
* @return { firstName: "henry", lastName: "arbolaez" }
*/
Top comments (0)