0

Am trying to display my data in a table form (Headers: key1 and key2). This is my code:

render() {
        console.log((this.state.message)); 
        const datamapping = Object.entries(this.state.message);
        console.log(datamapping);
        return (
            <div>
                <div className="viewall">
                    {datamapping.map((data, key) => {
                        return (
                            <div key={key}>
                                <p>{data.key1}</p>
                            </div>
                        );
                    })}
                </div>
            </div>
        );
    }

console.log(this.state.message) returns the following: {Items: Array(4), Count: 4, ScannedCount: 4}

In "Items":

Items: Array(4)
0: {key1: "value", key2: "value"}
1: {key1: "value", key2: "value"}
2: {key1: "value", key2: "value"}
3: {key1: "value", key2: "value"}

How should I go about being able to display the "values" in a table format? Currently, {data.key1} does not return any value.

2 Answers 2

2

Add ? after datamapping

{datamapping?.map((data, key) => {
    return (
      <div key={key}>
         <p>{data.key1}</p>
      </div>
   );
  })}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi sorry, it doesn't work. I still have a delay in the data resulting in the error
1

Use map for this.state.message.Items as Items is already an Array

this.state.message.Items.map()

3 Comments

I got this error: Cannot read property 'map' of undefined
I suspect that might be because my array is throwing a NULL array before it finally initialises and then gets processed
this.state.message.Items?.map() --- use this

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.