Advertisement
Guest User

Restructure array data

a guest
Apr 6th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 0.97 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>Testing</title>
  5.     <script type="text/javascript">
  6.         var data = ["whole", "2020-04-16", "2020-04-17","parking_a", "2020-04-16", "2020-04-16", "parking_b", "2020-04-17", "2020-04-17"];
  7.  
  8.         var chunkData = function(array, size) {
  9.            
  10.             // Check if array exists
  11.             if(!array) {
  12.                 return [];
  13.             }
  14.  
  15.             // Create first chunk
  16.             var firstChunk = array.slice(0, size);
  17.  
  18.             if(!firstChunk.length) {
  19.                 return array;
  20.             }
  21.  
  22.             return [firstChunk].concat(chunkData(array.slice(size, array.length), size));
  23.         }
  24.  
  25.         var getData = chunkData(data, 3);
  26.         var restructureData = [];
  27.  
  28.         getData.forEach(function(item, index) {
  29.            
  30.             restructureData[index] = {
  31.                 id: item[0],
  32.                 start_date: item[1],
  33.                 end_date: item[2]
  34.             };
  35.         });
  36.        
  37.         console.log('Array');
  38.         console.log(restructureData);
  39.         console.log(' ');
  40.         console.log('JSON')
  41.         console.log(JSON.stringify(restructureData));
  42.     </script>
  43. </head>
  44. <body>
  45.  
  46. </body>
  47. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement