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 trial
Kip Yin
4,847 PointsLoop over all methods in an object?
Say:
var foo = {
bar: x => x * 2,
baz: x => x / 2,
bam: x => x + 2,
};
and then something like (I'm not very acquainted with loops in JS):
for (let method; method in foo.keys()){
// for every method in `foo`
console.log(foo[method](5));
};
I just started learning JavaScript, and the syntax is probably wrong. But you get the idea...
2 Answers
Thomas Nilsen
14,957 PointsSure. You could do something like this:
var foo = {
bar: x => x * 2,
baz: x => x / 2,
bam: x => x + 2,
};
Object.keys(foo).forEach(method => console.log(foo[method](5)))
Thomas Nilsen
14,957 Pointsyes.
Kip Yin
4,847 PointsThx
Kip Yin
4,847 PointsKip Yin
4,847 PointsThanks for replying! Is
forEachan array method?