1
 get_bid() {
    let higest_bid_array:any[];
    for(let i=0; i < this.crypto.length;i++) { 
      higest_bid_array =  this.crypto[i].highestBid;
    }
    return higest_bid_array;
  }

I've declared the variable as an array.

higest_bid_array:any[];

Here crypto is an array with values for example: crypto[i].highestBid has the value [1234, 5647, 8500];

How do i assign the values of crypto to higest_bid_array;

higest_bid_array should return [1234, 5647, 8500], it now returns only 8500 i,e. the last value of that array.

where am i going wrong ? thanks.

  • Can you post your expected data structure? – Laiso Mar 19 '18 at 08:37
  • Possible duplicate of [Typescript - multidimensional array initialization](https://stackoverflow.com/questions/30144580/typescript-multidimensional-array-initialization) – Durga Mar 19 '18 at 08:38
  • use `higest_bid_array: number[][]`, check [here](https://www.tutorialspoint.com/typescript/typescript_multi_dimensional_arrays.htm) – Durga Mar 19 '18 at 08:40
  • higest_bid_array.push(this.crypto[i].highestBid) in for loop ? – mxr7350 Mar 19 '18 at 08:46
  • Can you please say, what exactly is going wrong? Is an error thrown? Is the assigned data wrong? – Markai Mar 19 '18 at 08:52
  • It outputs only the last value of that array, referring to the example above, it returns the last value **8500** – kimson Dooland Mar 19 '18 at 08:58
  • @mxr7350 thanks mate, i never noticed your comment. Your answer was right. :) Thanks guys it much helped. cheers – kimson Dooland Mar 19 '18 at 09:15

1 Answers1

1

You overwrite the array every time within your loop. You probably want to push values to the array within the for-loop.

get_bid() {
    let higest_bid_array:any[];
    for(let i=0; i < this.crypto.length;i++) { 
      higest_bid_array.push(this.crypto[i].highestBid);
    }
    return higest_bid_array;
  }

Read on here if you want to know more about the Array push method.

Philipp Meissner
  • 5,273
  • 5
  • 34
  • 59