0
Javascript
how can i loop inside an object in javascript?
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]
// ]
+ 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
+ 1
Got it! Thanksalot