I need help in here. I am confused how to get the object data in a proper way. When console.log(response.data.Data) My data looks like this :
Object {
"2014": Object {
"1": "3.385",
"10": "-0.191",
"11": "0.383",
"12": "0.191",
"2": "3.023",
"3": "3.667",
"4": "3.774",
"5": "-2.045",
"6": "2.088",
"7": "5.455",
"8": "-3.448",
"9": "16.741",
},
"2015": Object {
"1": "1.905",
"10": "5.092",
"11": "-4.070",
"12": "7.475",
"2": "5.421",
"3": "5.142",
"4": "-9.106",
"5": "4.824",
"6": "-4.425",
"7": "-2.963",
"8": "-1.527",
"9": "-4.845",
},
"2016": Object {
"1": "-1.504",
"10": "-1.115",
"11": "-7.891",
"12": "8.392",
"2": "2.863",
"3": "-1.299",
"4": "-1.880",
"5": "-0.383",
"6": "2.500",
"7": "8.443",
"8": "4.152",
"9": "4.319",
}
}
I cannot use console.log(response.data.Data.2014) or console.log(response.data.Data.object).
I want to get the year and the month object data. How do I get this object data ?
Thank You
CodePudding user response:
You can do like this
const data = {
"2014": {
"1": "3.385",
"10": "-0.191",
"11": "0.383",
"12": "0.191",
"2": "3.023",
"3": "3.667",
"4": "3.774",
"5": "-2.045",
"6": "2.088",
"7": "5.455",
"8": "-3.448",
"9": "16.741",
},
"2015": {
"1": "1.905",
"10": "5.092",
"11": "-4.070",
"12": "7.475",
"2": "5.421",
"3": "5.142",
"4": "-9.106",
"5": "4.824",
"6": "-4.425",
"7": "-2.963",
"8": "-1.527",
"9": "-4.845",
},
"2016": {
"1": "-1.504",
"10": "-1.115",
"11": "-7.891",
"12": "8.392",
"2": "2.863",
"3": "-1.299",
"4": "-1.880",
"5": "-0.383",
"6": "2.500",
"7": "8.443",
"8": "4.152",
"9": "4.319",
}
}
for (const year in data) {
console.log(`${year}: ${data[year]}`);
for (const month in data[year]) {
console.log(`${month}: ${data[year][month]}`);
}
}
and here is the document for it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in
CodePudding user response:
If we look at your object you are using an integer as the key. Though you have converted your integer to be a string still you can't use it as data.2014
. You have to use data["2014"]
.
✔ The data object's key name will only work as data.key
when your object will look like this:
const data = {
key: "Value"
}
or
const data = {
key2022: "Value"
}
✘ But it will not work when you use integer before in the key or you use an only integer to name your key and you need to use data["key"]
:
const data = {
"1key": "Value"
}
or
const data = {
"1234": "Value"
}
✔ So, If you want to get the value you have to use this code and try to use data["key"]
instead of data.key
for your object.
const data = {
"2014": {
"1": "3.385",
"10": "-0.191",
"11": "0.383",
"12": "0.191",
"2": "3.023",
"3": "3.667",
"4": "3.774",
"5": "-2.045",
"6": "2.088",
"7": "5.455",
"8": "-3.448",
"9": "16.741",
},
"2015": {
"1": "1.905",
"10": "5.092",
"11": "-4.070",
"12": "7.475",
"2": "5.421",
"3": "5.142",
"4": "-9.106",
"5": "4.824",
"6": "-4.425",
"7": "-2.963",
"8": "-1.527",
"9": "-4.845",
},
"2016": {
"1": "-1.504",
"10": "-1.115",
"11": "-7.891",
"12": "8.392",
"2": "2.863",
"3": "-1.299",
"4": "-1.880",
"5": "-0.383",
"6": "2.500",
"7": "8.443",
"8": "4.152",
"9": "4.319",
}
}
// To access the object of specific year use data["year"]
console.log(data["2014"])
// To access the value of specific month use data["year"]["month"]
console.log(data["2014"]["2"])