DEV Community

loading...

Object entries && fromEntrie

Henry Arbolaez
I'm a coffee lover, thinker, and a software engineer. I'm passionate about the idea of bringing people together to collaborate, to build a great product.
・1 min read

Learn about the Object.entries() and Object.fromEntrie() object method.


/**
 * Object.entries()
 *
 * @return method returns a two-dimensional array of a given object's.
*/

/**
 * Object.fromEntrie()
 *
 * @return methods return a object given a two-dimensional array.
*/

Enter fullscreen mode Exit fullscreen mode

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
*/

Enter fullscreen mode Exit fullscreen mode

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" } 
*/
Enter fullscreen mode Exit fullscreen mode

Discussion (0)