Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialTony Brackins
28,766 PointsIterate an array of objects
Wondering if anyone knows how to iterate an array of objects like this:
[{
title: 'title1',
description: 'description1'
}, {
title: 'title2',
description: 'description2'
}, {
title: 'title3',
description: 'description3'
}
]
2 Answers
Erwan EL
7,669 PointsIf you want to iterate the titles for example, you can use .map()
const array = [{
title: 'title1',
description: 'description1'
}, {
title: 'title2',
description: 'description2'
}, {
title: 'title3',
description: 'description3'
}
]
array.map(arr => arr.title)
Blayne Holland
19,321 PointsYou could use a for-loop like this.
var objArray = [{
title: 'title1',
description: 'description1'
}, {
title: 'title2',
description: 'description2'
}, {
title: 'title3',
description: 'description3'
}];
for(i = 0; i < objArray.length; i++) {
alert(objArray[i].title);
alert(objArray[i].description);
}