0

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

2 Answers 2

1

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

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

1 Comment

this one look easier
1

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"])

5 Comments

is it possible to do looping in this case ? I want to loop the response
Yes, it is possible. But you have to nest your loop. For example, you have to use a .forEach() to get the year's Object. And under that .forEach you have to run a for loop that's count starts from 1 and breaks when the value is 13
And to get years using forEach you can use the loDash. Please refer LoDash if you want to learn more about it
i don't know how to use LoDash
Then you can either read their docs by clicking on the link or you can use regular forEach instead of loDash's _.forEach

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.