I use the following function to dynamically check the properties of a variable number of objects.
// FUNCTION: Check Objects
var ObjectsN = 4;
function CheckObjects()
{
for (var i=0; i<=ObjectsN; i++)
{
if ((eval("Object"+i+".property")==0)
{
if (i==ObjectsN)
{
alert("Function finished");
}
}
else
{
return; // end function
}
}
}
I need to check if each object has the same property value.
Is there a way to do the same without using eval ?
A real example would be really much appreciated.
The structure you’re looking for is an
Array. So use an array to store your objects:Then testing for a property should implement
hasOwnPropertywhile iterating:You could then test against
objs[o].propertyto retrieve its value and see if it matches what you’re expecting.And for those browsers that may not have the function available, here is a cross-browser version of
hasOwnProperty(source, but originally from here):