Advertisement
lemansky

Untitled

Nov 27th, 2020
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.67 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <script>
  8.  
  9.         const json = [
  10.             {
  11.                 price_per_day: 2,
  12.                 period: [
  13.                   "2020-01-01",
  14.                   "2020-01-02",
  15.                   "2020-01-03",
  16.                   "2020-01-04",
  17.                   "2020-01-05",
  18.                   "2020-01-06"
  19.                 ]
  20.             },
  21.             {
  22.                 price_per_day: 60,
  23.                 period: [
  24.                   "2020-01-03",
  25.                   "2020-01-04",
  26.                   "2020-01-05",
  27.                   "2020-01-06",
  28.                   "2020-01-07",
  29.                   "2020-01-08"
  30.                 ]
  31.             },
  32.             {
  33.                 price_per_day: 15,
  34.                 period: [ "2020-01-05", "2020-01-06" ]
  35.             },
  36.             {
  37.                 price_per_day: 150,
  38.                 period: [
  39.                   "2020-01-08",
  40.                   "2020-01-09",
  41.                   "2020-01-10",
  42.                   "2020-01-11",
  43.                   "2020-01-12",
  44.                   "2020-01-13",
  45.                   "2020-01-14",
  46.                   "2020-01-15"
  47.                 ]
  48.             }
  49.         ];
  50.  
  51.         const arr = [];
  52.         const delItems = [];
  53.         for (let i = 0; i < json.length; i++) {
  54.             delItems.push(0);
  55.             for(let j = 0; j < json[i].period.length; j++){
  56.                 if(!arr.includes(json[i].period[j])){
  57.                     arr.push(json[i].period[j]);
  58.                     arr.push(i+"/"+j);
  59.                 }
  60.                 else if(arr.includes(json[i].period[j])){
  61.                     let _index = arr.indexOf(json[i].period[j]);
  62.                     let _indices = arr[_index+1].split("/");
  63.                     if(_indices[1] > j){
  64.                         json[_indices[0]].period.splice(_indices[1]-delItems[_indices[0]], 1);
  65.                         arr.splice(_index, 1);
  66.                         arr.splice(_index, 1);
  67.                         delItems[_indices[0]]++;   
  68.                         j--;               
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.  
  74.         console.log(json);
  75.  
  76.  
  77.     </script>
  78. </head>
  79. <body>
  80.    
  81. </body>
  82. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement