How can I split an array of objects and extract specific keys while adding 0s to fill up to a specific length?


let arr = [{
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "03.07.",
    "beginYear": "2023",
    "end": "10.07.",
    "endYear": "2023",
    "timestamp": 1688335200000
  }
}, {
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "26.06.",
    "beginYear": "2023",
    "end": "03.07.",
    "endYear": "2023",
    "timestamp": 1687730400000
  }
}, {
  "data": [5, 2],
  "date": {
    "begin": "19.06.",
    "beginYear": "2023",
    "end": "26.06.",
    "endYear": "2023",
    "timestamp": 1687125600000
  }
}, {
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "12.06.",
    "beginYear": "2023",
    "end": "19.06.",
    "endYear": "2023",
    "timestamp": 1686520800000
  }
}, {
  "data": [5, 2, 7, 2, 4, 2],
  "date": {
    "begin": "05.06.",
    "beginYear": "2023",
    "end": "12.06.",
    "endYear": "2023",
    "timestamp": 1685916000000
  }
}, {
  "data": [5],
  "date": {
    "begin": "29.05.",
    "beginYear": "2023",
    "end": "05.06.",
    "endYear": "2023",
    "timestamp": 1685311200000
  }
}];

arr.map((e) => {
  if (e.data.length > 4) {
    e.data = e.data.slice(0, 4);
  } else {
    let length = 4 - e.data.length;
    for (let i = 0; i < length; i++) {
      e.data.push(0);
    }
  }

})
console.log(arr);




My array of objects looks like this:
[{"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "26.06.", "beginYear": "2023", "end": "03.07.", "endYear": "2023", "timestamp": 1687730400000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "19.06.", "beginYear": "2023", "end": "26.06.", "endYear": "2023", "timestamp": 1687125600000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "12.06.", "beginYear": "2023", "end": "19.06.", "endYear": "2023", "timestamp": 1686520800000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "05.06.", "beginYear": "2023", "end": "12.06.", "endYear": "2023", "timestamp": 1685916000000}}, {"data": [5, 2, 7, 2, 4, 2], "date": {"begin": "29.05.", "beginYear": "2023", "end": "05.06.", "endYear": "2023", "timestamp": 1685311200000}}]

I want to map through the array, checking if the data key contains more than 4 numbers and if so, I want to extract the first 4 numbers (each data key is the same in all objects) and put it into a new object key "data" and the date key should look like the first item of the original array. The rest of the data key (in this example [4,2] should go into a new array that will be filled up with 0s until the data's length is 4. The date key of this should contain the date in 4 weeks starting from the first date.
I want to modify it so the result will be like this:
[{
    data: [ 5, 2, 7, 2 ],
    date: {
      begin: '29.05.',
      beginYear: '2023',
      end: '05.06.',
      endYear: '2023',
      timestamp: 1685311200000
    }
  },
{
    data: [4, 2, 0, 0 ],
    date: {
      begin: '26.06.',
      beginYear: '2023',
      end: '03.07.',
      endYear: '2023',
      timestamp: 1687730400000
    }
  }
]

The logic should also applicable if the data key contains more numbers
I tried it several times but somehow it doesn't as planned.
const fourStack = []

    const firstFourItems = result.slice(0, 4)
    const restItems = result.slice(4)

    const firstItem = firstFourItems[0]
    const newData = firstFourItems.map((item) => item.data.slice(0, 4))
    const newObj = {
      data: newData,
      date: {
        begin: firstItem.date.begin,
        beginYear: firstItem.date.beginYear,
        end: firstItem.date.end,
        endYear: firstItem.date.endYear,
        timestamp: firstItem.date.timestamp
      }
    }

    fourStack.push(newObj)

    restItems.forEach((item) => {
      fourStack.push({
        data: item.data.slice(0, 4),
        date: {
          begin: newObj.date.begin,
          beginYear: newObj.date.beginYear,
          end: newObj.date.end,
          endYear: newObj.date.endYear,
          timestamp: newObj.date.timestamp
        }
      })
    })

It gives me, not the result I want. Instead, it gives me this:
[{"data": [[Array], [Array], [Array], [Array]], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}, {"data": [5, 2, 7, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}, {"data": [5, 2, 7, 2], "date": {"begin": "03.07.", "beginYear": "2023", "end": "10.07.", "endYear": "2023", "timestamp": 1688335200000}}]

