0

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 ?

3
  • 2
    You have to assign the value, not store it in a variable, let allDates[slotDate] :any = []; should be allDates[slotDate] = []; to initialize an empty array at this index. Commented Dec 18, 2017 at 14:18
  • Why are you adding typing if you're setting it to any? Commented Dec 18, 2017 at 14:25
  • what you want? you want any data type or array data type? Commented Dec 18, 2017 at 14:30

2 Answers 2

2

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] = [];
      }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

This fixes their compilation/parsing problem, but you should warn them that their code is not using the benefits of a type system.
1

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] = [];
      }
    }
  }

4 Comments

what is the difference between let allDates: any[][] = []; and let allDates: any = [];??
const x: any[][] = 3 will fail compilation and so will const x: any[][] = [3]; Your code is saying that variable can take any type, my code is saying it has to be an array of arrays.
Am asking what about const x: any= 3 vs const x: any[][] = [3]?? because it compiling as same right?
@RameshRajendran Not sure what you are saying, I already explained to you that I'm adding actual types so that compiler can help you. const x: any[][] = [3]; will fail compilation because the compiler expects the array to contain other arrays, but you are putting in a number

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.