Using Object.entries to get iterable property-name pairs
Object.assign
works well for copying properties from one object to another. However, we sometimes want to perform other operations based on the properties of an object. This recipe shows how to use Object.entries
to get an iterable of an object's properties.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
06-02-object-entries-to-get-iterable
. - Copy or create an
index.html
that loads and runs amain
function frommain.js
. - Create a
main.js
with a function namedmain
that creates an object then uses afor-of
loop to loop over the result ofObject.entries
:
// main.js export function main() { const object = { foo: Math.random(), bar: Math.random() }; for (let [prop, value] of Object.entries(object...