Good day everyone. A cheerful api comes to me with a strict number of keys, 720, each of which consists of a set of values:

0: object x : 0 y : 1 z : 2 time : "2017-03-09T07:00:00+03:00" 1: object x : 0 y : 1 z : 2 time : "2017-03-09T07:00:02+03:00" ... [720] 

I need to consistently from the zero key to glue every 5 to a new one. As a result, you need to get 144 pieces, respectively, the time key in each new object should be 00:00 ; 01:00 ; 02:00 00:00 ; 01:00 ; 02:00

I pounded them forEach and _.each and maybe I’ve already gotten dirty, ask for help, send me, otherwise I don’t see where to go.

Example: what we have:

 0 [x:1, y:1, z:0, time: 00:00] 1 [x:2, y:2, z:4, time: 00:02] 2 [x:3, y:3, z:6, time: 00:04] 3 [x:0, y:0, z:0, time: 00:06] 4 [x:0, y:0, z:0, time: 00:08] ...720 

the output is the result of the addition of the first five:

 0 [x:6, y:6, z:10, time:00:00] 

the next iteration glues the next 5 pieces to the end

 1 [x:number, y:number, z:number, time: 00:10] 2 [x:number, y:number, z:number, time: 00:20] ...144 
  • 720/5 not at all 72. It is not clear exactly how you want to glue - Grundy
  • It was not right) 144 of course - Paralerodrom
  • Why time to do a string, if you can make an array? Is it implied that the values ​​of x,y,z same for every five, or how to glue? - teran
  • Add an example: for example, the initial array of 10 elements and the corresponding final array of two - Grundy
  • Described above how the mechanism should work - Paralerodrom

1 answer 1

 function ProcessEveryFive(input) { var result = []; for(var i = 0; i < input.length; i = i + 5) { var newItem = { x: input[i].x, y: input[i].y, z: input[i].z, time: input[i].time }; for (var j = i + 1; j < i + 5; j++) { newItem.x = newItem.x + input[j].x; newItem.y = newItem.x + input[j].y; newItem.z = newItem.x + input[j].z; } result.push(newItem); } return result; } 
  • Thanks a lot! It seems to be like a clock!) - Paralerodrom