Object.entries

Navigating and managing data structures is a really important skill for every level of engineer to have and improve upon. Over the years, the JavaScript language has continued to provide more methods for managing data structures, from Object.keys to Object.values and so on. One of my favorites is Object.entries, an API that provides the keys […]

The post Object.entries appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh

Navigating and managing data structures is a really important skill for every level of engineer to have and improve upon. Over the years, the JavaScript language has continued to provide more methods for managing data structures, from Object.keys to Object.values and so on. One of my favorites is Object.entries, an API that provides the keys and values via an array of arrays. Let’s have a look!

Consider the following object:

const obj = {
    name: "David",
    color: "green",
    balance: 100
}

Traditionally we’d have iterated over keys via a for loop, then use array syntax to get values:

const obj = {
    name: "David",
    color: "green",
    balance: 100
}

for (const key in obj) {
    const value = obj[key];
}

We do have Object.keys() and Object.values() to get each now, but neither method provides a relationship to the parent key or value. I really love using Object.entries to maintain that relationship and get both the key and value:

Object.entries({
    name: "David",
    color: "green",
    balance: 100
}).forEach(([key, value]) => console.log(key, value))

/*
name David
color green
balance 100
*/

Object.entries is such a useful method when you need both a key and value. Throw away those old for loops and Array-like syntaxes and use Object.entries like a pro!

The post Object.entries appeared first on David Walsh Blog.


This content originally appeared on David Walsh Blog and was authored by David Walsh


Print Share Comment Cite Upload Translate Updates
APA

David Walsh | Sciencx (2021-04-20T11:47:51+00:00) Object.entries. Retrieved from https://www.scien.cx/2021/04/20/object-entries/

MLA
" » Object.entries." David Walsh | Sciencx - Tuesday April 20, 2021, https://www.scien.cx/2021/04/20/object-entries/
HARVARD
David Walsh | Sciencx Tuesday April 20, 2021 » Object.entries., viewed ,<https://www.scien.cx/2021/04/20/object-entries/>
VANCOUVER
David Walsh | Sciencx - » Object.entries. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/20/object-entries/
CHICAGO
" » Object.entries." David Walsh | Sciencx - Accessed . https://www.scien.cx/2021/04/20/object-entries/
IEEE
" » Object.entries." David Walsh | Sciencx [Online]. Available: https://www.scien.cx/2021/04/20/object-entries/. [Accessed: ]
rf:citation
» Object.entries | David Walsh | Sciencx | https://www.scien.cx/2021/04/20/object-entries/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.