如何将对象数组推入另一个对象数组

时间:2019-10-11 10:13:29

标签: javascript node.js json promise

我有一个对象数组,试图将其附加到另一个对象数组中。我的对象数组是动态的,因此每次我运行该应用程序时,它都有一个不同的值,我想将其附加到另一个对象数组中。 我的对象数组如下:

[ { type: 'd',
    name: 'ABC',
    target: undefined,
    sticky: false,
    rights: { user: 'rwx', group: 'rx', other: 'rx' },
    acl: false,
    owner: 'ftp',
    group: 'ftp',
    size: 0,
    date: 2019-07-26T00:00:00.000Z },

  { type: 'd',
    name: 'DEF',
    target: undefined,
    sticky: false,
    rights: { user: 'rwx', group: 'rx', other: 'rx' },
    acl: false,
    owner: 'ftp',
    group: 'ftp',
    size: 0,
    date: 2019-08-02T00:00:00.000Z } ]

我想将对象的完整数组存储到另一个对象中。

ticketNameArr是我原始的对象数组。我尝试了以下方法将其附加到另一个对象数组。(已被注释的对象)

  Promise.all(ticketDetail).then(function (values) {
                //console.log(values);
                ticketNameArr.push({

                    TicketName: ticketNameIs,
                    InputLocationMessage: values[0].inputLocationMessage,
                    ResultLocationMessage: values[1].resultLocationMessage,
                    TicketStatusInput: values[0].ticketStatusInput,
                    TicketStatusResult: values[1].ticketStatusResult,
                    LogStatus: values[2].logStatus,
                    ReportStatus: values[3].reportStatus,
                    InputTime: values[0].birthtime,
                    InputModifiedTime: values[0].mtime,
                    OutputTime: values[1].birthtime,
                    OutputModifiedTime: values[1].mtime,
                    LogCreateTime: values[2].birthtime,
                    LogModifiedTime: values[2].mtime,
                    FtpTime: values[0].ftpTime,
                    FTPSTATUS: values[0].FTPStatus
                });




                if (ticketNameArr.length == ticketName.length) {

                    console.log("-------------TicketNameArr")
                    console.log(ticketNameArr)
                 ticketXLXSCopy = ticketXLXSCopy.concat(ticketNameArr)
                 console.log("-------------In  Excel")
                 console.log(ticketXLXSCopy)
                 var ws = XLSX.utils.json_to_sheet( ticketXLXSCopy)

                 XLSX.utils.book_append_sheet(wb, ws) // sheetAName is name of Worksheet

                 XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')


                //  var ws = XLSX.utils.json_to_sheet( ticketXLXSCopy)
                    // XLSX.utils.sheet_add_json(ws,ticketXLXSCopy);

                    // XLSX.utils.book_append_sheet(wb, ws) // sheetAName is name of Worksheet

                    // XLSX.writeFile(wb, 'DailyTicketSatus.xlsx')
                    // respond.render('result', { "ticketNameArr": ticketNameArr });
                    respond.render('index', { "ticketNameArr": ticketNameArr });




                }


            });```

The actual result should have all the fields of the original array of objects. While the methods that I tried are just overwriting it
Suppose ticketNameArr for the first time is 
[{name:"Priyanka",
  age:24}]
And after 4 hours say ticticketNameArr  is 
[{name:"Andrew",
  age:24}]
Then the array in which I want to append all the data should be like
[{name:"Priyanka",
  age:24},
{name:"Andrew",
  age:24}
]
The second array of objects should have all the data from the first one.As I mentioned ticketNameArr contains dynamic value and it stored different values after every 3-4 hours.
But every time my xlsx sheet has unappended values.i.e my ticketXLXSCopy is having only overwritten values.

2 个答案:

答案 0 :(得分:0)

Concat应该按照您的要求在这里工作。 var m = [{a:1},{b:2}]; var n = [{c:3},{d:4}]; 要将对象数组推入另一个对象,您可以尝试

  • 数组Concat方法(ES5)-> m = m.concat(n)
  • 扩展运算符(ES6)-> m = [...m, ...n]

再试一次。

答案 1 :(得分:0)

您可以使用Array Concat合并两个数组,

boat = Image.open('images\\venice-boat.jpg')

源链接:Link