2

For instance if I construct a new object type and create a few objects

    function website(name,users)
    {
        this.name = name;
        this.users = users;
    }

    var goog = new website("goog", "3,000,000");
    var fireFox = new website("fireFox", "1,000,000");
    var ie = new website("ie", "10");

And I push them into an array

    var websites = [];
    websites.push(goog,fireFox,ie);

Is there a way I can access a property of each object in the array through a loop? For instance

    for (var i=0;var<websites.length;i++)
        {
             console.log(websites[0.name]);
        }

I know this code doesn't work but I hope it clarifies what I'm trying to ask. Thanks!

koken
  • 25
  • 3

1 Answers1

1

When you say

websites[0.name]

It will try to get 0's name property, which is not valid. So, you should access it like this

websites[i].name

websites[i] will refer to the WebSite object in the array, at the index i and you are getting the name property with the . operator.

Also, your loop variable should be used in the for loop's condition, like this

for (var i=0; i < websites.length; i++)
thefourtheye
  • 233,700
  • 52
  • 457
  • 497