I'm trying to add a function to an array of objects, which each object has access to, but which does not need to be added to each object separately.
Let me give you a short example.
Let's say I have an array containing similar objects, each having a property x and a property y:
var objects = [{x:1, y:2},
{x:0, y:5},
{x:3, y:14}
];
I would like to calculate the sum of x
and y
for any of the objects.
First approach:
In order to calculate the sum for a given object, one could pass this object to a predefined function like so:
function xySum1(o) {return o.x + o.y;}
objects[0].x //--> returns 1
objects[0].y //--> returns 2
xySum1(objects[0]) //--> returns 3
This is quite ugly and unsatisfactory, as accessing the x
and y
properties is done differently. Also, my code is in different locations and the function xySum1
is not easily recognizable as being created to act on the objects in the array.
Second approach:
One could loop through the array and add the function as a property to each object:
for (var i=0; i < objects.length; i++) {
objects[i].xySum2 = function() {return this.x + this.y;};
}
Now, the sum is obtained by
objects[0].x //--> returns 1
objects[0].y //--> returns 2
objects[0].xySum2() //--> returns 3
which is much better.
Problems
There are, however, problems with this approach. Firstly, when I add a new element to the array
objects.push({x:5,y:21});
then the sum cannot be calculated before the function has been added to the object
objects[3].xySum2() //-->TypeError
Another problem is that I have many objects, and many functions to add to them. It seems like a waste of good memory space to add each function to each objects individually.
Is there a way to do this more efficiently?
Typically, when you want to execute a function on every element of an array, you use a for loop statement. JavaScript Array provides the forEach() method that allows you to run a function on every element. The forEach() method iterates over elements in an array and executes a predefined function once per element.
Method 1: Using the apply() method: The apply() method is used to call a function with the given arguments as an array or array-like object. It contains two parameters. The this value provides a call to the function and the arguments array contains the array of arguments to be passed.
The groupToMap() method groups the elements in an array using the values returned by its callback function. It returns a Map with the unique values from the callback function as keys, which can be used to access the array of elements in each group.
Define a class:
function MyObject(x, y) {
this.x = x;
this.y = y;
}
MyObject.prototype = {
xySum2: function() {
return this.x + this.y;
}, x: 0, y: 0
};
Then you can do:
var objects = [
new MyObject(1, 2),
new MyObject(0, 5),
new MyObject(3, 14)
];
objects[0].xySum2(); // 3
objects.push(new MyObject(5, 21));
objects[3].xySum2(); // 26
Alternatively, this is what Bergi probably wanted to say in his comment:
objects.xySum = function(index) {
return this[index].x + this[index].y;
};
This approach is a little bit "dirtier", but also less memory consuming.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With