Home > database >  Error: Objects are not valid as a React child when trying to map a nested array for a table componen
Error: Objects are not valid as a React child when trying to map a nested array for a table componen

Time:12-09

My code has been working fine so far. Until I had to modify the data file and create a nested array inside it. I am getting the Error: Objects are not valid as a React child (found: object with keys {one, B, C, D, E, G, H, I, L, M, N, P, Q, R, S}). If you meant to render a collection of children, use an array instead.

Below is my Table Component

import React from "react";
import tableData from "./tableData1.js";

const TableComponent = ({ data }) => {
let headings = Object.keys(data[0]);
  return (
    <table className="table table-dark table-striped">
      <thead>
        <tr>
          <th colspan="16">Dimensions in mm</th>
        </tr>
        <tr scope="col">
          <th>Series</th>
          {headings.map((heading) => (
            <th>{heading}</th>
          ))}
        </tr>
      </thead>
      <tbody>
        {data.map((item) => (
          <tr scope="col">
            <th scope="row">Series No.</th>
            {headings.map((heading) => (
              <td>{item[heading]}</td>
            ))}
          </tr>
        ))}
      </tbody>
    </table>
  );
};

const TableGenerator = ( {targetID} ) => {
  const filteredData = tableData.filter(item => item.id == targetID ).map(item => item.value);
  return <TableComponent data={filteredData} />;
};

export default TableGenerator;

And here is the Data File currently using only some mock data for testing.

const tableData1 = [
  {
    id: 1,
    value: [
      {
        one: "Value",
        B: "Value",
        C: "Value",
        D: "Male",
        E: "Value",
        G: "Value",
        H: "Value",
        I: "Value",
        L: "Value",
        M: "Value",
        N: "Value",
        P: "Value",
        Q: "Value",
        R: "Value",
        S: "Value",
      },
      {
        one: "Value",
        B: "Value",
        C: "Value",
        D: "Male",
        E: "Value",
        G: "Value",
        H: "Value",
        I: "Value",
        L: "Value",
        M: "Value",
        N: "Value",
        P: "Value",
        Q: "Value",
        R: "Value",
        S: "Value",
      },
    ],
  },
  {
    id: 2,
    value: [
      {
        two: "Value",
        B: "Value",
        C: "Value",
        D: "Male",
        E: "Value",
        G: "Value",
        H: "Value",
        I: "Value",
        L: "Value",
        M: "Value",
        N: "Value",
        P: "Value",
        Q: "Value",
        R: "Value",
        S: "Value",
      },
      {
        two: "Value",
        B: "Value",
        C: "Value",
        D: "Male",
        E: "Value",
        G: "Value",
        H: "Value",
        I: "Value",
        L: "Value",
        M: "Value",
        N: "Value",
        P: "Value",
        Q: "Value",
        R: "Value",
        S: "Value",
      },
    ],
  }
]

Previously I had it in the format below:

const tableData1 = [
  {
    id: 1,
    value:
      {
        one: "Value",
        B: "Value",
        C: "Value",
        D: "Male",
        E: "Value",
        G: "Value",
        H: "Value",
        I: "Value",
        L: "Value",
        M: "Value",
        N: "Value",
        P: "Value",
        Q: "Value",
        R: "Value",
        S: "Value",
      }
  },
  {
    id: 2,
    value:
      {
        two: "Value",
        B: "Value",
        C: "Value",
        D: "Male",
        E: "Value",
        G: "Value",
        H: "Value",
        I: "Value",
        L: "Value",
        M: "Value",
        N: "Value",
        P: "Value",
        Q: "Value",
        R: "Value",
        S: "Value",
      }
  }
]

I'm guessing I have to map it differently or something along those lines, but I can't seem to wrap my head around it no matter where I look. Anyone able to help out on this?

CodePudding user response:

Since in your new structure value is itself an array, actually you have a nested array, you can update your filterData like this:

const filteredData = tableData.filter(item => item.id == targetID).reduce((acc, item)=>{
  acc = [...acc, ...item.value]
  return acc;
} , []);
  • Related