0

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hi Thank you, it was placing the declaration afterwards that did the trick :-) G
0

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 :)

1 Comment

Hi you're correct, but to solve the problem I had to place declaration of the array afterwards :-) G

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.