0

Heres is how I add data to my object:

    let trade = [
       new RecordStock(elem, recordtime, stockquantity, tradetype, stocksymbol),
   ]

The problem is when I run this:

 console.log(trade[0].price);

It seems that overwrites my object from the beginning.

I add my class code. How i can print the first data from the object

    class RecordStock {

  constructor(price, timestamp, stockquantity, tradetype, stocksymbol) {
    this.price = price;
    this.timestamp = timestamp;
    this.stockquantity = stockquantity;
    this.tradetype = tradetype;
    this.stocksymbol = stocksymbol;
  }
  static recordTrade(){
    console.log(price[0]);
  }
}
4
  • What do you mean by overwrite? The object inside the array will stay as is from the time you initialised the array. Commented Nov 26, 2017 at 21:19
  • @NanduKalidindi when I execute a function and console.log(trade[0]) it always print a different object. Example: Input: price => 100. When I save it in an object it works perfect but when I want save more prices in the object like 101 I want when I print price[0] keeps the number 100 and NO the 101. Commented Nov 26, 2017 at 21:25
  • I am not sure I fully understand but are you trying to add objects to the trade array as you execute a function? Commented Nov 26, 2017 at 21:29
  • Yes but I want to push the information creating a new object but not overwrite the data in the object. Commented Nov 26, 2017 at 21:32

2 Answers 2

1

I think you are looking to push objects into the trade array. You can achieve this by simply appending to the array.

You can push objects with something like this

Assuming you have a function that adds the RecordStock object. Here is how it could be.

let trade = [];    

trade.push(new RecordStock(elem, recordtime, stockquantity, tradetype, stocksymbol));

trade.push(new RecordStock(elem, recordtime, stockquantity, tradetype, stocksymbol));

trade.push(new RecordStock(elem, recordtime, stockquantity, tradetype, stocksymbol));

Now you can access the latest RecordStock you pushed using this trade[trade.length - 1]. trade[0] will always contain the object which you pushed first. This is the usual array functionality.

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

7 Comments

the name of the class is called RecordStock
Did you initialise trade using let trade = []?
no...now works the problem is that iam printing trade[0] that always is the data i put but trade.object[0] i dont know how i can print
I have added my class
I updated the answer a little. But I am still un clear as to what you want to achieve. Please post a working snippet if this did not solve your issue.
|
0

The problem is that inside the function I declare de var every time. I put let trade out of the function

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.