简体   繁体   English

将对象数组转换为另一个结构化对象数组

[英]Convert array of objects to another structured array of objects

I am pretty new to JS and React, and can't convert two things as following:我对 JS 和 React 很陌生,不能转换如下两件事:

myData array of objects: myData对象数组:

[ 
   0: {"last_name: "somebody last name", "name": "somebody name",.....},
   2: {"last_name: "somebody last name", "name": "somebody name",...},
   3: ....
]

I need convert them with:我需要将它们转换为:

let xlsxDataSource = myData.map(item => {
        return {
        
        value: item.last_name,
        value: item.name
      };
    });

to the array of objects where the keys are all is named value :键所在的对象数组被命名为value

[
   0: {"value: "somebody last name"},
   2: {"value: "somebody name"},
   3: {"value: "somebody middle_name"},
   4: {"value: "somebody time entrance"},
   5: ...
]

This is required by react library which converts this array of objects to xlsx file.这是将这个对象数组转换为 xlsx 文件的反应库所必需的。

My code rewriting the prev value and gives only:我的代码重写了 prev 值并仅给出:

[
   0: {"value: "somebody name"},
   2: {"value: "somebody name"},
   3: {"value: "somebody name"},
   4: {"value: "somebody name"},
   5: ...
]

How can I do these transofmations properly.我怎样才能正确地进行这些转换。 Any ideas please?请问有什么想法吗?

Upd.更新。

Such structure is required by library : 图书馆需要这样的结构:

const multiDataSet = [
    {
        columns: [
            {title: "Headings", width: {wpx: 80}},//pixels width 
            {title: "Text Style", width: {wch: 40}},//char width 
            {title: "Colors", width: {wpx: 90}},
        ],
        data: [
            [
                {value: "H1", style: {font: {sz: "24", bold: true}}},
                {value: "Bold", style: {font: {bold: true}}},
                {value: "Red", style: {fill: {patternType: "solid", fgColor: {rgb: "FFFF0000"}}}},
            ],
            [
                {value: "H2", style: {font: {sz: "18", bold: true}}},
                {value: "underline", style: {font: {underline: true}}},
                {value: "Blue", style: {fill: {patternType: "solid", fgColor: {rgb: "FF0000FF"}}}},
            ],
            [
                {value: "H3", style: {font: {sz: "14", bold: true}}},
                {value: "italic", style: {font: {italic: true}}},
                {value: "Green", style: {fill: {patternType: "solid", fgColor: {rgb: "FF00FF00"}}}},
            ],
            [
                {value: "H4", style: {font: {sz: "12", bold: true}}},
                {value: "strike", style: {font: {strike: true}}},
                {value: "Orange", style: {fill: {patternType: "solid", fgColor: {rgb: "FFF86B00"}}}},
            ],
            [
                {value: "H5", style: {font: {sz: "10.5", bold: true}}},
                {value: "outline", style: {font: {outline: true}}},
                {value: "Yellow", style: {fill: {patternType: "solid", fgColor: {rgb: "FFFFFF00"}}}},
            ],
            [
                {value: "H6", style: {font: {sz: "7.5", bold: true}}},
                {value: "shadow", style: {font: {shadow: true}}},
                {value: "Light Blue", style: {fill: {patternType: "solid", fgColor: {rgb: "FFCCEEFF"}}}}
            ]
        ]
    }
];

And the one below is myData from console.log:下面是来自 console.log 的 myData:

(25) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {last_name: "...", first_name: "...", middle_name: "...", monh: "2021-02-01 00:00:00", name: "OITIB", …}
1: {last_name: "...", first_name: "... ", middle_name: "...", monh: "2021-02-01 00:00:00", name: "OITIB", …}
2: {last_name: "...", first_name: "...", middle_name: "...", monh: "2021-01-31 00:00:00", name: "OITIB", …}
3: {last_name: "...", first_name: "...", middle_name: "...", monh: "2021-01-29 00:00:00", name: "OITIB", …}
....
length: 25
__proto__: Array(0)

You can use Array.prototype.flatMap for this.您可以为此使用Array.prototype.flatMap It applies a callback where you return an array and all of the returned arrays are concatenated together (flattened).它应用一个回调,您返回一个数组,并且所有返回的 arrays 连接在一起(展平)。

In our callback, we get all of the values for each object as an array and then map them to objects with property value .在我们的回调中,我们将每个 object 的所有值作为数组获取,然后将 map 获取到具有属性value的对象。

 const myData = [ {last_name: "somebody last name", name: "somebody name"}, {last_name: "somebody last name", name: "somebody name"}, ] const res = myData.flatMap( obj => Object.values(obj).map( value => ({value}) ) ); console.log(res);

Edit: It turns out that we don't actually want to flatten the data, If each element is a row of your dataset.编辑:事实证明,如果每个元素都是数据集的一行,我们实际上并不想展平数据。 then we want to return an array of arrays.然后我们要返回一个数组数组。 So everything in the above code stays the same except replace .flatMap with .map .因此,上述代码中的所有内容都保持不变,只是将.flatMap替换为.map

Let's add in the columns too, which we can get from the first element (will cause an error if the array is empty).让我们也添加列,我们可以从第一个元素中获取(如果数组为空将导致错误)。

 const myData = [ {last_name: "somebody last name", name: "somebody name"}, {last_name: "somebody last name", name: "somebody name"}, ] const multiDataSet = [ { columns: Object.keys(myData[0]).map( key => ({title: key}) ), data: myData.map( obj => Object.values(obj).map( value => ({value}) ) ) } ] console.log(multiDataSet);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM