Home > Net >  Array of arrays to array of Objects
Array of arrays to array of Objects

Time:12-24

enter image description here

Hi I have question. From the above can i be able to convert the data to somethin like

const events = [
        {
          createdAt: "2021-12-06T16:32:59.260Z"
      funeralId: "0d327e48-b083-4bf5-8dc9-f690c681febd"
    id: "4090aa7e-713b-42b3-a31a-92adb69b9cdb"
    location: "Off-Site"
    name: "Viewing"
    roomId: null
    state: "not_started"
    streamEnd: "2021-12-08T08:14:00.000Z"
    streamId: "Last1638947340"
    streamStart: "2021-12-08T07:09:00.000Z"
    updatedAt: "2021-12-06T16:32:59.260Z"
        },
        {
          createdAt: "2021-12-06T16:32:59.260Z"
      funeralId: "0d327e48-b083-4bf5-8dc9-f690c681febd"
    id: "4090aa7e-713b-42b3-a31a-92adb69b9cdb"
    location: "Off-Site"
    name: "Viewing"
    roomId: null
    state: "not_started"
    streamEnd: "2021-12-08T08:14:00.000Z"
    streamId: "Last1638947340"
    streamStart: "2021-12-08T07:09:00.000Z"
    updatedAt: "2021-12-06T16:32:59.260Z"
        },
    
    ..........
]

if anyone knows please answer

CodePudding user response:

Use Array.flat method

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified dept

const arr = [[1,2,3],[4,5]];

const res = arr.flat()

console.log(res)

CodePudding user response:

You can use Array.flat()

const events = [
      [{ createdAt: "2021-12-06T16:32:59.260Z",
        funeralId: "0d327e48-b083-4bf5-8dc9-f690c681febd",
        id: "4090aa7e-713b-42b3-a31a-92adb69b9cdb",
        location: "Off-Site",
        name: "Viewing",
        roomId: null,
        state: "not_started",
        streamEnd: "2021-12-08T08:14:00.000Z",
        streamId: "Last1638947340",
        streamStart: "2021-12-08T07:09:00.000Z",
        updatedAt: "2021-12-06T16:32:59.260Z" 
      }], 
      [{ 
        createdAt: "2021-12-06T16:32:59.260Z",
        funeralId: "0d327e48-b083-4bf5-8dc9-f690c681febd",
        id: "4090aa7e-713b-42b3-a31a-92adb69b9cdb",
        location: "Off-Site",
        name: "Viewing",
        roomId: null,
        state: "not_started",
        streamEnd: "2021-12-08T08:14:00.000Z",
        streamId: "Last1638947340",
        streamStart: "2021-12-08T07:09:00.000Z",
        updatedAt: "2021-12-06T16:32:59.260Z" 
      }]
    ].flat();
console.log(events)

  • Related