4

I have an JSON-Object as follows:

Input for the months is customerSend,customerReceived,totalSendAllCustomers,totalReceivedAllCustomers

var emailObj = {
                "kundenNummer":17889,
                "jahre":
                {
                    2017:{
                        "Januar":[15,30,75,125],
                        "Februar":[17,32,77,127],
                        "März":[19,34,79,129],
                    },
                    2018:{
                        "Januar":[28,12,66,198],
                        "Oktober":[40,4,40,5],
                    }
                }
        }

How exactly do I access the specific year? I already tried it like this:

var keysYears = Object.keys(emailObj.jahre);
        var currentSelectedYear = keysYears[0];

        var keysMonth = Object.keys(emailObj.jahre[currentSelectedYear]);   
        var currentSelectedMonth = keysMonth[0];

        document.write(emailObj.jahre[currentSelectedYear].2017[0]);

I also tried some other ways of doing this but I already deleted those.

Can you tell me how to access the 2017 or 2018 data? I know that I could convert them into strings but I want to know if I could also do it this way.

1
  • Do emailObj.jahre['2017'].Januar[0] Commented Mar 21, 2017 at 7:35

4 Answers 4

4

You can call the properties of your object emailObj by their names.

Either with a dot notation

emailObj.kundenNummer

Or by brackets notation

emailObj["kundenNummer"]

The dot notation won't work in your case because the name of your property is a number. You should then use

emailObj.jahre["2017"]

var emailObj = {
  "kundenNummer": 17889,
  "jahre": {
    "2017": {
      "Januar": [15, 30, 75, 125],
      "Februar": [17, 32, 77, 127],
      "März": [19, 34, 79, 129],
    },
    "2018": {
      "Januar": [28, 12, 66, 198],
      "Oktober": [40, 4, 40, 5],
    }
  }
};

let year = "2017";
let month = "Januar";

console.log(emailObj.jahre[year][month]);

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

1 Comment

Thanks, this gave me a good clarification of when to use dots and when to use Brackets.
1

You should use bracket notation.

document.write(emailObj.jahre[currentSelectedYear][currentSelectedMonth][0]);

var emailObj = {
                "kundenNummer":17889,
                "jahre":
                {
                    2017:{
                        "Januar":[15,30,75,125],
                        "Februar":[17,32,77,127],
                        "März":[19,34,79,129],
                    },
                    2018:{
                        "Januar":[28,12,66,198],
                        "Oktober":[40,4,40,5],
                    }
                }
        }
var keysYears = Object.keys(emailObj.jahre);
var currentSelectedYear = keysYears[0];
var keysMonth = Object.keys(emailObj.jahre[currentSelectedYear]);   
var currentSelectedMonth = keysMonth[0];
document.write(emailObj.jahre[currentSelectedYear][currentSelectedMonth][0]);

Comments

0

In a JavaScript object, the key is always a string, even if you use an integer it will be converted into a string.

obj = {
    key1: //contents
    key2: //contents
}

To access a specific key:

obj.key1
obj['key1']

For your example:

emailObj.jahre['2017']
emailObj['jahre']['2017']

Use the for in looping construct to loop through the keys of an object:

var emailObj = {
    "kundenNummer":17889,
    "jahre": {
        2017:{
            "Januar":[15,30,75,125],
            "Februar":[17,32,77,127],
            "März":[19,34,79,129],
        },
        2018:{
            "Januar":[28,12,66,198],
            "Oktober":[40,4,40,5],
        }
    }
}

for (key in emailObj.jahre) {
    console.log(emailObj.jahre[key]) //Here key will be '2017', '2018' etc
}

Comments

0

You cannot access with dot notation properties which contain as name a number in JavaScript. Instead you should consider using bracket notation.

Example:

emailObj.jahre['2017']

var emailObj = {
  "kundenNummer": 17889,
  "jahre": {
    2017: {
      "Januar": [15, 30, 75, 125],
      "Februar": [17, 32, 77, 127],
      "März": [19, 34, 79, 129],
    },
    2018: {
      "Januar": [28, 12, 66, 198],
      "Oktober": [40, 4, 40, 5],
    }
  }
};
console.log(emailObj['jahre']['2017']);
console.log(emailObj.jahre['2017']);

Comments

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.