猿问

如何在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.如果我想将另一个对象FinalData以给定格式(如对象数组)附加到数据数组,则在 Header1 下,从 Data01 到 Data04 的数据同样应该出现。我们怎样才能做到这一点?FinalData的格式为


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"  }],

            ]

我想要将 FinalData 映射到 ExcelData 中的数据的函数,格式应该相同。因为我需要在 dataSet={ExcelData} 中传递相同的结构以获得预期的输出 Excel。


export default function ExcelExportTwo() {

    return (

        <div>

            <ExcelFile element={<button>ExcelExportThree</button>}>

                <ExcelSheet dataSet={ExcelData} name="Organization" />

            </ExcelFile>

        </div>

    );

}

Excel 工作表中的预期输出应该是

手掌心
浏览 164回答 1
1回答

隔江千里

我不确定这是否是您所需要的。const ExcelData = [&nbsp; {&nbsp; &nbsp; columns: [&nbsp; &nbsp; &nbsp; [{title: 'Header1'}],&nbsp; &nbsp; &nbsp; [{title: 'Header2'}],&nbsp; &nbsp; &nbsp; [{title: 'Header3'}],&nbsp; &nbsp; &nbsp; [{title: 'Header4'}],&nbsp; &nbsp; ],&nbsp; &nbsp; data: [],&nbsp; },];const FinalData = [&nbsp; [&nbsp; &nbsp; {value: 'Data11'},&nbsp; &nbsp; {value: 'Data12'},&nbsp; &nbsp; {value: 'Data13'},&nbsp; &nbsp; {value: 'Data14'},&nbsp; ],&nbsp; [&nbsp; &nbsp; {value: 'Data21'},&nbsp; &nbsp; {value: 'Data22'},&nbsp; &nbsp; {value: 'Data23'},&nbsp; &nbsp; {value: 'Data24'},&nbsp; ],&nbsp; [&nbsp; &nbsp; {value: 'Data31'},&nbsp; &nbsp; {value: 'Data32'},&nbsp; &nbsp; {value: 'Data33'},&nbsp; &nbsp; {value: 'Data34'},&nbsp; ],&nbsp; [&nbsp; &nbsp; {value: 'Data41'},&nbsp; &nbsp; {value: 'Data42'},&nbsp; &nbsp; {value: 'Data43'},&nbsp; &nbsp; {value: 'Data44'},&nbsp; ],];const result = [{...ExcelData[0], data: [...FinalData]}];console.log(result);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答