简体   繁体   中英

Declare array of array in loop typescript

On my app I have an object of dates which contains time array like Console log output is as follows

32: {
1514160000: Array [ 1200, 1500 ],
1514764800: Array [ 1200, 1500 ],
1515369600: Array [ 1200, 1500 ],
1515974400: Array [ 700, 1200, 1500 ],
1516579200: Array [ 700, 1200, 1500 ],
}

With this data I have implemented loop to create a new array of dates with time and worker id as follows ( similar to this )

1514160000 :[
1200 : [32,40,56],
1500 : [32,40],
],
1514764800: [
1200 : [32,40,56],
1500 : [32,40],
]

I have written following code for this where I want to create array of dates by dynamically assign dates and then create it an array again.

let allDates :any = [];
      for(let pid in this.allAvailableProviders)
      {
        console.log(pid);
        for(let slotDate in this.allAvailableProviders[pid]){
          if(!Array.isArray(allDates[slotDate])){
            let allDates[slotDate] :any = [];
          }
        }
      }

where allAvailableProviders is object

It gives me following error on ng serve

'=' expected

How can I do it ?

You are just initializing the array. There is no assignment or need for let:

    for(let slotDate in this.allAvailableProviders[pid]){
      if(!Array.isArray(allDates[slotDate])){
        allDates[slotDate] = [];
      }
    }

The let statement is followed by a variable name, not an expression.

You should just define your outer array as the right type, instead of any . You know it's an array of arrays.

  let allDates: any[][] = [];
  for(let pid in this.allAvailableProviders)
  {
    console.log(pid);
    for(let slotDate in this.allAvailableProviders[pid]){
      if(!Array.isArray(allDates[slotDate])){
        // You'd get an error if you assigned something that is not an array.
        // because you specified the type correctly
        allDates[slotDate] = [];
      }
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM