LOOP
for-of and for-in
1 | for (const item of restaurant.starterMenu) console.log(item); |
Item is always the current element in each iteration.
Index is always the current index or property name in each iteration.
You can also use
if-else
tobreak
orcontinue
the loop.
Acess the item and index at the same time
1 | for (const item of restaurant.starterMenu.entries()) console.log(item); |
So you can see we used entries()
method in for-of loop
entries()
- An Array Iterator
1 | console.log(...restaurant.starterMenu.entries()); |
- Object.entries(objectName)
1 | console.log(Object.entries(restaurant)); |
Each Array is composed of the key and the value.
If you want to loop a object
Object.keys(objectName)
This method returns an Arrary composed by the object’s property names.
1 | console.log(Object.keys(restaurant)); |
Object.values(objectName)
This method returns an Arrary composed by the object’s property values.
1 | console.log(Object.values(restaurant)); |