Reputation: 5
I have a array of objects and I want to select a property of a certain object in that array. When I try the following code, it doesn't work, I don't get any value in the string:
var _string = teams[2].name;
Below the code of the array:
var teams = new Array (team1, team 2, team3);
var team1 = {
name: "team 1",
matches: 5
}
var team2 = {
name: "team 2",
matches: 4
}
var team3 = {
name: "team 3",
matches: 3
}
Some help would be great :-)
Thanks
G
Upvotes: 0
Views: 393
Reputation: 219
There is error var teams = new Array (team1, team 2, team3);
and should be var teams = new Array (team1, team2, team3);
You add unnecessary space between team
and 2
:)
Upvotes: 0
Reputation: 6957
You should declare your teams first and your array afterwards like this:
var team1 = {
name: "team 1",
matches: 5
}
var team2 = {
name: "team 2",
matches: 4
}
var team3 = {
name: "team 3",
matches: 3
}
var teams = [team1, team2, team3];
Also note there is a space between team
and 2
in your code which is incorrect.
Upvotes: 3