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.

7
  • Can you post your expected data structure? Commented Mar 19, 2018 at 8:37
  • Possible duplicate of Typescript - multidimensional array initialization Commented Mar 19, 2018 at 8:38
  • use higest_bid_array: number[][], check here Commented Mar 19, 2018 at 8:40
  • higest_bid_array.push(this.crypto[i].highestBid) in for loop ? Commented Mar 19, 2018 at 8:46
  • Can you please say, what exactly is going wrong? Is an error thrown? Is the assigned data wrong? Commented Mar 19, 2018 at 8:52

1 Answer 1

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.

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.