skip to Main Content

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


  1. newData shouldn’t be a map, but a slice of the data of firstItem. You’re getting all the data of all items in firsFourItems by doing a map.

    You’re pushing the same date in your forEach and the data you’re using is still the first four of the original data key. It should use a slice of the initial data and filled with 0 until the length is 4.

    Consider:

    for(var k = 0; k < result[0].data.length / 4; ++k){
        newstack.push({
            data:Array.from({...result[0].data.slice(4 * k),length:4},(v,i)=>v??0),
            date: result[k*4].date // Edit: since you want the date sorted, sort the dates then get the [k*4] of the sorted array
        })
    }
    
    Login or Signup to reply.
  2. 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);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search