1

i have json array like this

EMAIL: (3) ["[email protected]", "[email protected]", "[email protected]"]
FIRSTNAME: (3) ["John", "Harry", "Howard"]
LASTNAME: (3) ["Smith", "Pierce", "Paige"]
SMS: (3) ["33123456789", "33111222222", "33777888898"]

I am trying to create a table like this

enter image description here

then group of firstName , LastName and so on

in render() i have tried

{columnMapRows.EMAIL.map(row => (
    <TableRow >
    <TableCell align="center">{row}</TableCell>
    </TableRow>
    ))}

But i dont want to map with column name , i am looking for generic solution . email , firstname , lastname are dynamic , coming form csv file , they can be changed anytime

i have also tried foreach and simple array loop . but it doesnt work Please suggest

3
  • Are you using any specific UI library? Commented Nov 5, 2019 at 9:41
  • @Neeraj Verma what is your required output? Commented Nov 5, 2019 at 9:43
  • what you are getting in row for each EMAIL???? Commented Nov 5, 2019 at 9:45

1 Answer 1

1

You should use the index within your map function to access other data.

{columnMapRows.EMAIL.map((email, i) => (    
    <TableRow key={email}>
     <TableCell align="center">{email}</TableCell>
     <TableCell align="center">{columnMapRows.FIRSTNAME[i]}</TableCell>
     <TableCell align="center">{columnMapRows.LASTNAME[i]}</TableCell>
     <TableCell align="center">{columnMapRows.SMS[i]}</TableCell>
    </TableRow>
))}
// this assumes that all the array are the same length which seems to be the case

EDIT: dynamic solution assuming email is always there

{columnMapRows.EMAIL.map((email, i) => (    
    <TableRow key={email}>
     {Object.keys(columnMapRows).map((key) => (
        <TableCell align="center" key={key} >{columnMapRows[key][i]} </TableCell>
     ))}
    </TableRow>
))}

EDIT: dynamic solution with data refactor

const keys = Object.keys(columnMapRows);
const items = columnMapRows[keys[0]].map(() => ({}));
keys.forEach((key) => {
  columnMapRows[key].forEach((data, i) => {
     items[i][key] = data;
  });
});

{items.map((item) => (    
    <TableRow>
      {Object.keys(item).map((key) => (
         <TableCell key={key}>{item[key]}</TableCell>
      ))}
    </TableRow>
))}

This could certainly be improved but I think it shows the main idea. I decided to construct an array of object as I think it's pretty easier yo understand. Then iterate on those object properties using Object.keys.

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

11 Comments

email , firstname , lastname are dynamic , coming from csv file , they can be changed anytime
You mean you can have for example phone instead of sms or some data without lastname ?
yes , instead of lastname , their can be another column like phone
its wrong , why firstname , lastname come inside email ?
in columnMapRows.EMAIL.map ... EMAIL want to come dynamically
|

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.