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 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}}]
2
Answers
newData
shouldn’t be a map, but a slice of thedata
offirstItem
. You’re getting all thedata
of all items infirsFourItems
by doing a map.You’re pushing the same
date
in yourforEach
and thedata
you’re using is still the first four of the originaldata
key. It should use a slice of the initialdata
and filled with 0 until the length is 4.Consider: