Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
- const json = [
- {
- price_per_day: 2,
- period: [
- "2020-01-01",
- "2020-01-02",
- "2020-01-03",
- "2020-01-04",
- "2020-01-05",
- "2020-01-06"
- ]
- },
- {
- price_per_day: 60,
- period: [
- "2020-01-03",
- "2020-01-04",
- "2020-01-05",
- "2020-01-06",
- "2020-01-07",
- "2020-01-08"
- ]
- },
- {
- price_per_day: 15,
- period: [ "2020-01-05", "2020-01-06" ]
- },
- {
- price_per_day: 150,
- period: [
- "2020-01-08",
- "2020-01-09",
- "2020-01-10",
- "2020-01-11",
- "2020-01-12",
- "2020-01-13",
- "2020-01-14",
- "2020-01-15"
- ]
- }
- ];
- const arr = [];
- const delItems = [];
- for (let i = 0; i < json.length; i++) {
- delItems.push(0);
- for(let j = 0; j < json[i].period.length; j++){
- if(!arr.includes(json[i].period[j])){
- arr.push(json[i].period[j]);
- arr.push(i+"/"+j);
- }
- else if(arr.includes(json[i].period[j])){
- let _index = arr.indexOf(json[i].period[j]);
- let _indices = arr[_index+1].split("/");
- if(_indices[1] > j){
- json[_indices[0]].period.splice(_indices[1]-delItems[_indices[0]], 1);
- arr.splice(_index, 1);
- arr.splice(_index, 1);
- delItems[_indices[0]]++;
- j--;
- }
- }
- }
- }
- console.log(json);
- </script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement