How do I call a method on an array of objects in javascript?

jack_rancid

How do I call a method on an array of objects in javascript? I created an object with certain properties and methods. Then created additional objects. I set up my additional objects in an array. Now, I want to use a for loop to call a method on each of the objects in the array without having to write console.log each time. Thanks.

//create object Rabbit with adjective property and describeMyself method

function Rabbit(adjective) {
    this.adjective = adjective;
    this.describeMyself = function() {
    console.log("I am a " + this.adjective + " rabbit");
    };
}

//create three new objects in Rabbit class

var rabbit1 = new Rabbit("fluffy");
var rabbit2 = new Rabbit("happy");
var rabbit3 = new Rabbit("sleepy");

//create array of objects for rabbit class
var rabbits = [rabbit1, rabbit2, rabbit3];

//attempt to log describeMyself method for each object in the rabbits array using a for     
// loop.

for(i = 0; i < rabbits.length; i++){
console.log([i].describeMyself());
}
JLRishe

What you're trying to do is this:

console.log(rabbits[i].describeMyself());

But there's a problem here, because describeMyself() itself logs to the console and doesn't return anything, so what you would see in the console is:

I am a fluffy rabbit
undefined
I am a happy rabbit
undefined
I am a sleepy rabbit
undefined

To remedy this, I suggest changing the definition of Rabbit to the following:

function Rabbit(adjective) {
    this.adjective = adjective;
}

Rabbit.prototype.describeMyself = function() {
    return "I am a " + this.adjective + " rabbit";
};

so that it returns a string instead. Placing the function on the prototype, while a completely separate matter, should offer you some performance benefits as well since you won't be making a new copy of describeMyself for each Rabbit.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I call abstract class' child class method when using an array of child objects?

How do i call dispose method(if it exists) on all objects being GC'ed in javascript

How do I merge an array of objects in Javascript?

How do I call a method for all objects in an ArrayList?

How do I declare the objects having array of objects in JavaScript?

Javascript: Call method on object in array of objects

How do I create an boolean equals method compares objects in an array

How do I convert this php array into a JavaScript array of objects?

How do I filter an array object inside of a array of objects Javascript?

How do I call a method on each object inside of an array?

How do I call a method of a Java instance from JavaScript?

How do I call a Vue method into a plain javascript function?

How do I call a dynamically-named method in Javascript?

How do I call a dynamically-named method with for in Javascript

How do I call a javascript method from angular js?

How do I remove objects from a JavaScript associative array?

How do I list Javascript array of objects in HTML

Javascript - How do I map an array of objects to a datatable with column names?

How do I modify an object value in a cloned array of objects in Javascript

How do I convert array of Objects into one Object in JavaScript?

How do I create a javascript array of lists of objects?

How do I convert an array of objects into an object (dynamically) in JavaScript

How do I reuse objects in an array for a particle system in JavaScript/jQuery?

How do I convert array of objects to object in javascript?

How do I check if a particular object exist or not in a JavaScript array of objects?

How do I return an array of objects with certain conditions in Javascript?

How do I convert array of Objects to one Object in JavaScript?

How do I filter and count objects in a javascript array?

How do I create an object from array of objects using javascript?