Search code examples
javascriptreactjsreact-data-table-component

How to add an object to another object in array in javascript


import React from 'react';
import ReactExport from 'react-data-export';

const ExcelFile = ReactExport.ExcelFile;
const ExcelSheet = ReactExport.ExcelFile.ExcelSheet;

ExcelData = [{
        columns:
            [
                [{ title: "Header1" }],
                [{ title: "Header2" }],
                [{ title: "Header3" }],
                [{ title: "Header4" }],
            ],
    data:
        [
          [ {..}, {..}, {..}, {..} ],
          [ {..}, {..}, {..}, {..} ],
          [ {..}, {..}, {..}, {..} ],
          [ {..}, {..}, {..}, {..} ],  
        ],    

}]

1.if i want to append the another object FinalData to the data array in given format like array of objects , under Header1 the Data from Data01 to Data04 likewise should come. how can we do that? The format of the FinalData will be

FinalData = [
                [{ value: "Data01" },
                 { value: "Data02" },
                 { value: "Data03" }
                 { value: "Data04"  }],
                [{ value: "Data21" },
                 { value: "Data22" },
                 { value: "Data23" }
                 { value: "Data24"  }],
                [{ value: "Data31" },
                 { value: "Data32" },
                 { value: "Data33" }
                 { value: "Data34"  }],
                [{ value: "Data41" },
                 { value: "Data42" },
                 { value: "Data43" }
                 { value: "Data44"  }],
            ]

I want the function which maps the FinalData to the data in ExcelData, the format should be the same. Because i need to pass the same structure in dataSet={ExcelData} to get the expected output Excel.

export default function ExcelExportTwo() {
    return (
        <div>
            <ExcelFile element={<button>ExcelExportThree</button>}>
                <ExcelSheet dataSet={ExcelData} name="Organization" />
            </ExcelFile>
        </div>
    );
}

Expected Output in the Excel Sheet should be enter image description here


Solution

  • I am not sure if this is what you need.

    const ExcelData = [
      {
        columns: [
          [{title: 'Header1'}],
          [{title: 'Header2'}],
          [{title: 'Header3'}],
          [{title: 'Header4'}],
        ],
        data: [],
      },
    ];
    
    const FinalData = [
      [
        {value: 'Data11'},
        {value: 'Data12'},
        {value: 'Data13'},
        {value: 'Data14'},
      ],
      [
        {value: 'Data21'},
        {value: 'Data22'},
        {value: 'Data23'},
        {value: 'Data24'},
      ],
      [
        {value: 'Data31'},
        {value: 'Data32'},
        {value: 'Data33'},
        {value: 'Data34'},
      ],
      [
        {value: 'Data41'},
        {value: 'Data42'},
        {value: 'Data43'},
        {value: 'Data44'},
      ],
    ];
    
    const result = [{...ExcelData[0], data: [...FinalData]}];
    console.log(result);