0

I am trying to add elements in an Array in Typescrypt with the push method, but it does not seem to work. the array remains empty. This is my code:

         list: Array<int> = Array(10)
         for(let x = 0; x <= 10; x++) {
                    list.push(x)
         }

someone got the same problem?

1
  • 1
    That code shouldn't compile, int isn't a keyword in typescript (number is). I also assume that list is defined as a class member and the for-loop is done somewhere else? Doesn't make sense otherwise. Commented Sep 25, 2018 at 12:01

3 Answers 3

2

in your case you can do :

     list: Array<number> = [];
     for(let x = 0; x <= 10; x++) {
          list.push(x)
     }

or

     list: Array<number> = Array(10)
     for(let x = 0; x <= 10; x++) {
          list[x];
     }

Explanation on your error :

Array(10) already creates an array with 10 "empty" elements.

if you use push on it, you will actually get your elements pushed, but in the 11th to the 20th position.

The 1st to the 10th ranks stay empty (and will return undefined if you try to get their value them)

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

2 Comments

moreover, as MohanaRajesh noticed, you use int instead of number
I know int does not exists I changed this in stackoverflow, not in my IDE. The datatype is acctually a more complicated datatype, but can't share that on the internet. Cause that will expose what I am working on. ;)
2

So a few things to note:

  • There's no int type in TypeScript, since JavaScript has only one number type, the corresponding TypeScript type is called number.
  • You shouldn't use Array(n) (or the array constructor in general) to create an array, there's a lot of information about why that is (primarily, it creates what's called a sparse array, with a length property but no elements), but you should generally use [] to create a new array. All arrays in JavaScript are dynamic anyway, so the 10 you pass has no meaning.
  • You should never define variables without declaring them with const, let or var.

Combining the points above, here's how your code should look like for this case

const list: number[] /* or Array<number> */ = []
for(let x = 0; x <= 10; x++) {
  list.push(x)
}

2 Comments

I know int does not exists I changed this in stackoverflow, not in my IDE. The datatype is acctually a more complicated datatype, but can't share that on the internet. Cause that will expose what I am working on. ;)
@HarryStylesheet You can't ask a question with code that doesn't compile and is not equivalent to the code you actually have, and expect helpful answers.
1

int type is not available in typescript use number instead of int

let list: Array<number> =  Array(10);
for (let x = 0; x <= 10; x++) {
  list.push(x)
}

above code is pushing the value to array but this will return

[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

to fix this please change code to

let list: Array<number> =  Array();
for (let x = 0; x <= 10; x++) {
  list[x] = x;
}

this will return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

1 Comment

I know int does not exists I changed this in stackoverflow, not in my IDE. The datatype is acctually a more complicated datatype, but can't share that on the internet. Cause that will expose what I am working on. ;)