Home > Mobile >  Retrieving data within an object in react JS
Retrieving data within an object in react JS

Time:10-03

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

I have a set of data that looks like this, where typeof = object:

{"body", "Items":[{"key1":"value","key2":"value"},{"key1":"value","key2":"value"}],"Count":2,"ScannedCount":2}

I want to display it in a table format, but I am unable to access the values inside items. How should I go about doing it? I tried to do a JSON parse, but there was an error. Doing {data[1]} gets me to "Items", but I am unable to access the data inside.

CodePudding user response:

let's say you have the following parameter:

const x = {"body":"test", "Items":[{"key1":"value","key2":"value"},{"key1":"value","key2":"value"}],"Count":2,"ScannedCount":2}

now you can access Items as follow:

const items = x.Items;

and extract each element of it as follow:

const item0 = items[0];
//or each item inside of each element
const key1 = item0.key1;
const key2 = item0.key2;

let me know if you have any problems.

  • Related