0

I have an object called at.sports.

And my object contains two fields: sporttype - string , and team which is an array ...or another object.

it's something like this:

at.sports = {
    sporttype : "Soccer"
    team : [
               { name : "bayern FC",
                 caption: "FCB"
               }
           ]
    }

If i check the structure in visual studio i get:

 at.sports = {
    proto: {},
    sporttype: "Soccert"
    team: object Object
}

and if i click on team.. i see

0 - name - "arsenal" , caption - "Asnl" 1 - name - "bayern FC" , caption - "FCB" I want to access the team property.. check every index (0,1) for caption and see if is set. If caption is undefined.. i want to replace it with the value of name and than capitalize the first letter.

ex: for index 0 if i have only name ="arsenal" and caption unset , i need that caption to become "arsenal", and than capitalize first letter .. so at the end caption = "Arsenal" .

I really don't know how to .. get my hands on the right property and modify.

And i'm very curious.. the team property.. is an object or an array? i think it's an object because i get object Object in Visual studio when i click on at.spors ...

Oh..trust me..i researched on google. I just don't understand how to access what i need from inside at.sports.

After i acces what i need..i see there is a method for capitalize.. and it's easy to change a property value with other value. I get errors in for statement.

I tried this:

       for (var i = at.sports.length; i--;) {
            if (typeof at.sports.team[i] === "undefined") {
                alert("something is undefined");
            }
       }

And i get the alert 2 times. I have 2 indexes. So..i'm thinking the problem wrong.

Matt
  • 37
  • 1
  • 4
  • Have a look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Predefined_Core_Objects#Array_Object and http://stackoverflow.com/q/11922383/218196 – Felix Kling Jun 18 '14 at 04:17

1 Answers1

0

If at.sports is not an array, as the object definition suggests up top, then this is what you need:

var teamArray = at.sports.team;
for(var i = 0; i < teamArray.length; i++) {
    if(teamArray[i].name != null && teamArray[i].caption == null) {
        var n = teamArray[i].name;
        teamArray[i].caption = n[0].toUpperCase() + n.substring(1);
    }
}

If it is an array, as your code sample suggests, then this:

for(var i = 0; i < at.sports.length; i++) {
    var teamArray = at.sports[i].team;
    for(var j = 0; j < teamArray.length; j++) {
        if(teamArray[j].name != null && teamArray[j].caption == null) {
            var n = teamArray[j].name;
            teamArray[j].caption = n[0].toUpperCase() + n.substring(1);
        }
    }
}

In the future, I would organize the question and information better. Also, as suggested by the comment to your question, I would spend some time reading up on JavaScript.

Allen G
  • 1,160
  • 6
  • 8
  • i know i know.. and i'm very sorry. i just need to figure this out fast.. because i have an exam in 30 mins... and i will have to modify nested properties. thank you very much for the help.. and i hope i will get rid of that -1 rep. (i'm too low to aford that) – Matt Jun 18 '14 at 04:28