Home > Mobile >  Iterating an array and replacing values
Iterating an array and replacing values

Time:12-16

I want to set this.arr.items to 1 when this.inputObject.timeTable will return values from the map to me, e.g .: I will give an example because it is difficult to explain it to me For example:

  "timeTable":{
    "0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"16:00"}],
    "1": [{"from":"00:00","to":"05:00"}]
    "2": [],
    "3": []
    "4": [],
    "5": []
    "6": [] 
  }

i want to set this.arr.items:

 this.arr.items= [
    [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
    [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
  ];

My code:

  arr: Row[] = [
    { items: new Array(24).fill(1),active: true},
    { items: new Array(24).fill(1),active: true},
    { items: new Array(24).fill(1),active: true},
    { items: new Array(24).fill(1),active: true},
    { items: new Array(24).fill(1),active: true},
    { items: new Array(24).fill(1),active: true},
    { items: new Array(24).fill(1),active: true},
  ];


 if(this.inputObject.timeTable){
    for (let [key, value] of this.inputObject.timeTable.entries()) {
      this.arr.forEach(el=>{
        el.items.fill(0)
        el.active=false
      })
      value.forEach((el,ind)=>{
         for(let i = parseInt(el.from); i<parseInt(el.to) 1; i  ){
          this.arr[key].items[i] = 1
        }
      })
      }
    }

Unfortunately it doesn't work All my code: https://stackblitz.com/edit/angular-ivy-2lveus?file=src/app/app.component.ts&fbclid=IwAR23d8PtUNJpMAOmqvFbe3PAQQhtNzydHSfSQv_UEsi7E3DKMPgv6PffXqg

CodePudding user response:

You can get the hour part from from and to key for each object and then replace the value to 1 for every value in that range.

const timeTables = { "0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"16:00"}], "1": [{"from":"00:00","to":"05:00"}], "2": [], "3": [], "4": [], "5": [], "6": [] },
    getHours = (time) => parseInt(time.split(':')[0], 10)
    result = Object.entries(timeTables).reduce((r, [key, value], i) => {
      r[i] = Array(24).fill(0);
      value.forEach(o => {
        let start = getHours(o.from);
        const end = getHours(o.to);
        while(start <= end) {
          r[i][start] = 1;
          start  ;
        }
      })
      return r;
    }, []);
console.log(result.map(arr => arr.join(',')).join('\n'));
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

Join time slot array based on from and to by spread operator ... or Array.concat.

const config = {"timeTable":{
    "0": [{"from":"08:00","to":"12:00"}, {"from":"14:00","to":"16:00"}],
    "1": [{"from":"00:00","to":"05:00"}],
    "2": [],
    "3": [],
    "4": [],
    "5": [],
    "6": [] 
  }}
  
  function getTimeArray(timeTable) {
    const emptyArray = Array.from({length: 24}).map(() => 0)
    return Object.entries(timeTable).map(([key, value]) => {
      let result = emptyArray.slice()
      value.forEach((item) => {
        const beg = item.from.split(':')[0] * 1
        const to = item.to.split(':')[0] * 1
        result = [...result.slice(0, beg), ...result.slice(beg, to   1).map(() => 1), ...result.slice(to   1)]
      })
      return result
    })
  }
  console.log(
    JSON.stringify(getTimeArray(config.timeTable))
  )

  • Related