0

I am new to React JS & currently trying to iterate a certain data to present the same in react js but not able to do the same. The data which looks something like this

data

Now, the final output should be look something like this in tabular format

desired-result

The things which I tried are:- tried

tried2

The error which I am getting below one is for 1 image and for second, the code is not getting parsed.

[![error][5]][5]

So, how to achieve the desired output in react js

7
  • What did you tried already ? Commented Feb 5, 2021 at 14:32
  • @TheTisiboth I have updated my question. Please have a look. Thanks. Commented Feb 5, 2021 at 14:40
  • Can you please avoid using images, but instead just copy past your code (you can format it by selecting it and doing CTRL + K) It is much more easyer to read and modify it for us Commented Feb 5, 2021 at 14:41
  • What does listData looks like, when you print it ? Commented Feb 5, 2021 at 14:43
  • listdata looks like ``` Meeting Room 1:[ 0: {field_name: "SSID Name", value: "xfcsd", section_name: "Meeting Room 1"} 1: {field_name: "name", value: "sdfdsf", section_name: "Meeting Room 1"} ] Meeting Room 2:[ 0: {field_name: "SSID Name", value: "sdfds", section_name: "Meeting Room 2"} 1: {field_name: "name", value: "sdfdsf", section_name: "Meeting Room 2"} ] Meeting Room 3:[ 0: {field_name: "SSID Name", value: "sdfsdf", section_name: "Meeting Room 3"} 1: {field_name: "name", value: "sdfdsfs", section_name: "Meeting Room 3"} ] ``` Commented Feb 5, 2021 at 14:50

1 Answer 1

1

It looks like you're trying to use map onto an object, while you should use it on a collection. Maybe try something like this :

Object.values(listData.data).map((meetingRoom) => { // your code here });

This will allow you to use the content inside your data object as an array of objects.


Edit : Sorry, I didn't understand you need to access the key as well as the value. To achieve that you can simply use Object.entries which will return the key (Meeting Room 1, Meeting Room 2 in this instance) in the first variable and the array of items in the second variable.

Here's a quick example:

Object.entries(listData.data).forEach(([key, value]) => {
    console.log(key, value);
    // You could use value.map() to iterate over each object in your meeting room field array.
});

Note : you can also use a for (... of ...) loop like this instead of a forEach :

for (const [key, value] of Object.entries(listData.data)) {
    console.log(key, value);
};

For more information about the Object.entries method, feel free to check the MDN Webdocs page about it here.

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

3 Comments

Thanks for the response but in your case I will not be getting the heading Meeting Room 1 heading for each table as shown in the above image.
I've updated my initial answer to provide a more suited solution. Let me know if you have any issues or questions !
Thanks for the response. I needed to do only a small modification to make it work for me in react environment:- Object.entries(listData.data).forEach(([key, value]) => { // You could use value.map() to iterate over each object in your meeting room field array. }); Object.entries(listData.data).map(([key, value]) => { // You could use value.map() to iterate over each object in your meeting room field array. });

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.