How can I push the result of a function into the properties of an object?
See the output below. I’m trying to push() the valueCalc() result to the portfolio object property “value: undefined”
const portfolio = {
_stocks: [],
get stock() {
return this._stocks;
},
addStocks(newTicker, newPrice, newQuantity, newValue) {
let newStock = {
ticker: newTicker,
price: newPrice,
quantity: newQuantity,
value: newValue
}
this._stocks.push(newStock);
}
};
portfolio.addStocks('PMT', 1.40, 100, );
portfolio.addStocks('PMT', 1.37, 100, );
portfolio.addStocks('PMT', 1.52, 100, );
portfolio.addStocks('WR1', 1.90, 100, );
portfolio.addStocks('WR1', 1.80, 100, );
portfolio.addStocks('WR1', 1.88, 100, );
const valueCalc = () => {
for (let i = 0; i < portfolio.stock.length; i++) {
console.log(portfolio.stock[i].price * portfolio.stock[i].quantity)
}
};
console.log(portfolio.stock);
valueCalc();
OUTPUT:
[
{ ticker: 'PMT', price: 1.4, quantity: 100, value: undefined },
{ ticker: 'PMT', price: 1.37, quantity: 100, value: undefined },
{ ticker: 'PMT', price: 1.52, quantity: 100, value: undefined },
{ ticker: 'WR1', price: 1.9, quantity: 100, value: undefined },
{ ticker: 'WR1', price: 1.8, quantity: 100, value: undefined },
{ ticker: 'WR1', price: 1.88, quantity: 100, value: undefined }
]
140
137
152
190
180
188