0

Javascript

how can i loop inside an object in javascript?

27th Feb 2019, 12:23 PM
mwai Galo
mwai Galo - avatar
3 Antworten
+ 11
Looping through objects in JavaScript Way to loop through objects is: • First to convert the object into an array. • Then, you loop through the array. You can convert an object into an array with three methods: Object.keys Object.values Object.entries ▪Object.keys creates an array that contains the properties of an object. const fruits = { apple: 28, orange: 17, pear: 54, } const keys = Object.keys(fruits) console.log(keys) // [apple, orange, pear] ▪Object.values creates an array that contains the values of every property in an object. const fruits = { apple: 28, orange: 17, pear: 54, } const values = Object.values(fruits) console.log(values) // [28, 17, 54] ▪Object.entries creates an array of arrays. Each inner array has two item. The first item is the property; the second item is the value. const fruits = { apple: 28, orange: 17, pear: 54, } const entries = Object.entries(fruits) console.log(entries) // [ // [apple, 28], // [orange, 17], // [pear, 54] // ]
27th Feb 2019, 2:44 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 2
.... for( var objKey in obj){ ... obj[objKey].... } ..... Please, dont post multiple identical questions in so few time. It can be seen like spam and thats NOT tollerated here
27th Feb 2019, 12:32 PM
KrOW
KrOW - avatar
+ 1
Got it! Thanksalot
1st Mar 2019, 8:00 AM
mwai Galo
mwai Galo - avatar