2

Hi I'm new in ReactJS Framework. I have problem regarding how to get the value of sec1 object and sec2 object to the API Response. and how to render return the value to my html page.

I have here my constructor codes and my response data.

constructor(){
    super();
    this.state = {
        sec1: [],
        sec2: []
    }
}
componentDidMount() {

        axios.get('/api/community').then(response => {
           this.setState({
               sec1: response.data,
               sec2: response.data
           })
        })

    }

My Console Log

my 2 object

5
  • 1
    sec1: response.data[0].sec1[0] sec2: response.data[0].sec2[0] Commented Sep 13, 2018 at 14:20
  • Thanks @ChrisAdams Commented Sep 13, 2018 at 14:23
  • it's should be sec1: response.data[0].sec1 and sec2: response.data[0].sec2 Commented Sep 13, 2018 at 14:33
  • @OlivierBoissé let me try Commented Sep 13, 2018 at 14:41
  • @OlivierBoissé yah. same output. without [0] Commented Sep 13, 2018 at 14:42

2 Answers 2

2

Difficult without a clearer view of the data, but something like this perhaps?

sec1: response.data[0].sec1[0]
sec2: response.data[0].sec2[0]

Not ideal, but you could try drilling down bit by bit with console.log statements until you get what you need.

To me your data looks like...

array

prop

array

json object

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

2 Comments

why sec1: response.data[0].sec1[0] and not sec1: response.data[0].sec1 ?
Because, or at least to me, the screenshot looks like sec1 is an array.
0

Adding to the above answers:

Option 1:

sec1: response.data[0].sec1[0]
sec2: response.data[0].sec2[0]

Will store sec1 and sec2 as objects. Mainly used if you know that sec1 and sec2 can never have more than one object and you need to drill down into each object in your application.

Option2 :

sec1: response.data[0].sec1
sec2: response.data[0].sec2

will store sec1 and sec2 as arrays. Mainly used when sec1 and sec2 are collection of objects and you need to iterate over each object to display/manipulate the data.

Choose wisely depending on your use-case.

2 Comments

hi shivam the option 2 is working to my html page.. however if i use the option it gives me error.
If you post some code/details about the error, I can take a look at it.

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.