1
Hi !

I have a nested array of arrays of objs{timestamps} and i need to give a max of 3-objs per each sub-arrays and when it reaches it limit feel up a new array without mixing the timestamps

for example i have a sub-group of 5 objs on the same Date i need 1sub-group of 3obs an another of 2obj

Data i have

const results = 
 [
   [
    {id: 3337, data: "data", date: '2022-08-02T12:25:00.000Z'},
    {id: 3336, data: "data", date: '2022-08-02T12:24:00.000Z'},
    {id: 3335, data: "data", date: '2022-08-02T12:23:00.000Z'},
    {id: 3334, data: "data", date: '2022-08-02T12:22:00.000Z'}
   ],
   [
    {id: 4449, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4448, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4447, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4446, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4445, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4444, data: "data", date: '2022-07-15T12:25:00.000Z'},
   ],
   [
    {id: 1117, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1116, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1115, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1114, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1113, data: "data", date: '2022-07-07T12:25:00.000Z'},
   ]
  ]

Expected output :

i don't know if its super clear but i need them to stay in the same order and if they don't share the same timestamps well stays alone.

    const results = 
 [
   [
    {id: 3337, data: "data", date: '2022-08-02T12:25:00.000Z'},
    {id: 3336, data: "data", date: '2022-08-02T12:24:00.000Z'},
    {id: 3335, data: "data", date: '2022-08-02T12:23:00.000Z'},
   ],
   [
    {id: 3334, data: "data", date: '2022-08-02T12:22:00.000Z'}
   ],
   [
    {id: 4449, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4448, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4447, data: "data", date: '2022-07-15T12:25:00.000Z'},
   ],
   [
    {id: 4446, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4445, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4444, data: "data", date: '2022-07-15T12:25:00.000Z'},
   ],
   [
    {id: 1117, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1116, data: "data", date: '2022-07-15T12:25:00.000Z'},
   ],
   [
    {id: 1115, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1114, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1113, data: "data", date: '2022-07-07T12:25:00.000Z'},
   ],
  ]
Momo
  • 67
  • 7
  • I'm traying some code with rasults.map() but not good enough to share it for the moment. – Momo Oct 17 '22 at 11:24
  • Is the `results` array already partitioned by `date`? – Nick Oct 17 '22 at 11:24
  • Yes results array is already partitioned by date – Momo Oct 17 '22 at 11:25
  • Use one of the functions in the duplicate to chunk an array (call it `array_chunk`) and then `results = map(array_chunk, results)` – Nick Oct 17 '22 at 11:28
  • For example: `const chunks = 3; const array_chunk = (list) => Array.from({length : Math.ceil(list.length / chunks) }, (_,i) => list.slice(i*chunks,(i+1)*chunks)); const output = results.map(array_chunk)` – Nick Oct 17 '22 at 11:41
  • Should be `flatMap`, not `map` in the above comments – Nick Oct 17 '22 at 11:48

1 Answers1

2

You can using slice() to do it

let data = 
 [
   [
    {id: 3337, data: "data", date: '2022-08-02T12:25:00.000Z'},
    {id: 3336, data: "data", date: '2022-08-02T12:24:00.000Z'},
    {id: 3335, data: "data", date: '2022-08-02T12:23:00.000Z'},
    {id: 3334, data: "data", date: '2022-08-02T12:22:00.000Z'}
   ],
   [
    {id: 4449, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4448, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4447, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4446, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4445, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 4444, data: "data", date: '2022-07-15T12:25:00.000Z'},
   ],
   [
    {id: 1117, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1116, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1115, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1114, data: "data", date: '2022-07-15T12:25:00.000Z'},
    {id: 1113, data: "data", date: '2022-07-07T12:25:00.000Z'},
   ]
  ]

let result = data.map(d => {
  let index = 0
  let arr =[]
  while(index < d.length){
    arr.push(d.slice(index,index+3))
    index = index + 3
  }
  return arr
})

console.log(result.flat())
flyingfox
  • 13,414
  • 3
  • 24
  • 39
  • Maybe using `slice()` is a better choice – flyingfox Oct 17 '22 at 11:39
  • I closed this as a dupe 6 minutes *before* you posted this answer... – Nick Oct 17 '22 at 11:42
  • @Nick I am wondering why you have closed it and I still can edit my answer? – flyingfox Oct 17 '22 at 11:46
  • Once you've answered you can edit as much as you like regardless of the state of the question. But you shouldn't be able to *answer* after the question is closed... – Nick Oct 17 '22 at 11:47
  • Hi Nick stackoverflow sais that these questions has been already asked (i don't agreed ) and it redirect me to https://stackoverflow.com/questions/8495687/split-array-into-chunks – Momo Oct 17 '22 at 12:07
  • i tried changing the name of the questions but still it continue to sais is the same question – Momo Oct 17 '22 at 12:07
  • 1
    @Momo it has been closed and you can not reopen it by yourself,also please check my answer – flyingfox Oct 17 '22 at 12:13
  • Hi guys.. as a junior dev i can only say thank a lot !! I really appreciate your help. You save me in these one ! I'm doing many trials and it looks promising.. if you want i can do a repository with all the code to show you de final result. – Momo Oct 17 '22 at 13:44