0

I have a demo here - https://stackblitz.com/edit/angular-w7vavy?file=src%2Fapp%2Fapp.component.ts

I have a simple function that creates an array of objects.

If I do a typeof on the array it says it's an object.

I'm I doing something wrong, is it an object or array, I need t to be an array

createData() {
    this.testData = [];
    console.log(typeof(this.testData));
    for(let n:number=0; n<=this.dates.length-1;n++){
      for(let i:number= 0; i<=4; i++){
        this.testData[i] = {
          data_1: Math.floor(Math.random() * (this.max - this.min)),
          data_2: Math.floor(Math.random() * (this.max - this.min)),
          data_3: Math.floor(Math.random() * (this.max - this.min)),
          data_4: Math.floor(Math.random() * (this.max - this.min)),
          date: this.dates[i]
        }
      }
    }
    console.log(this.testData)
    console.log(typeof(this.testData));
  }

1 Answer 1

10

Because an array, strictly speaking, is an object. Just a very complex one.

Array.isArray(val) will tell you if an object is an array.

Sign up to request clarification or add additional context in comments.

Comments

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.