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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Daniel Salvatori](https://secure.gravatar.com/avatar/501db991bef37d7aab900384f6395865?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Daniel Salvatori
2,658 PointsAdding a method to multiple objects within an array
Hello, I need help with a task.
Say i have an array which contains a large number of objects. How can I create function which will add a method to multiple (or even all) objects with in the array?
For example a simple print method.
Thank you
2 Answers
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsIf these objects are all instances of a particular class, you can add the method to the class prototype which will give it to every item at once:
// add a "print" method to every instance of "thing":
thing.prototype.print = function (arg) {
/* your method body code */
}
If you need more specific help, please show your code.
![Daniel Salvatori](https://secure.gravatar.com/avatar/501db991bef37d7aab900384f6395865?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Daniel Salvatori
2,658 PointsGreat, one question remaining would be how to access the values inside the "actors" array and get them to print using this. method?
I've created a function- getActors(title) - to print these values previously but i do not know how to apply it to the context of this method.
here is the code for getActors if interested
var actorNameArray = [];
var actorFirstObj = '';
function getActors (title){
for ( var i = 0; i < movieData.length; i ++){
if (title === movieData[i].title){
for(var j = 0; j < movieData[i].actors.length; j++)
actorFirstObj += movieData[i].actors[j].firstName + ' ' +movieData[i].actors[j].lastName + ', ';
actorNameArray.push(actorFirstObj);
for(var k = 0; k < actorNameArray.length; k++){
console.log(actorNameArray[k]);
}
}
}
}
getActors('Back to the Future');
using getActors.this(title) prints every actor from every movie for each instance.
Thanks in advance.
![Steven Parker](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 PointsThe "getActors" is already an example of the kind of function I was suggesting. If you want the output to go to the page you could substitute "document.write
" for "console.log
":
document.write(actorNameArray[k]);
I tested it with the previously supplied data:
getActors("The Lord of the Rings: The Two Towers");
Daniel Salvatori
2,658 PointsDaniel Salvatori
2,658 PointsHi Steven,
thanks for the reply.
The Array looks like this:
And I want to have the method print all the details of each movie.
Steven Parker
230,688 PointsSteven Parker
230,688 PointsThese are anonymous objects. So you could create a definition for them, and then define the data as new instances of that type, or you could create the prototype method on the generic object "Object":
Also, instead of a method you could create an ordinary function that takes one of these objects as an argument:
Daniel Salvatori
2,658 PointsDaniel Salvatori
2,658 PointsI appreciate your help Steven, thank you!