简体   繁体   中英

How can I select a Object which has an Array as part of its constructor?

How can I access a certain argument in a Object constructor which is an Array and pick a index in the array to do a calculation with (get the total of all the items for that costumer).

I have been trying to get the price value in the Items Object and add the total for each costumer object in the order Array.

I am selecting them from a selection element in HTML, which is populated in JS.

console.log(costomer[0].order[1]);

I have tried various syntax but when I choose the index of the costumer array I get undefined as the result.

//Waiter Constructor
function Costumer(name, order, total) {
  this.name = name;
  this.order = [];
  this.total = total;
}

//CostomerArray
const costumers = [
  new Costumer('Timo'),
  new Costumer('Bill')
];

//This is done twice in the same way for the items as well both populate the 
//selection element.
custumer.forEach(({ name }) => costumerEl.options.add(new Option(name)));

//Item constuctor
function Item(item, price) {
  this.item = item;
  this.price = price;
}

//Main food array
const items = [
  new Item('Keyboard', 14.50),
  new Item('mouse', 10)
];

//This is the function which adds the items to the array when the form is submitted.
const formEl = document.getElementById('mainForm');

formEl.onsubmit = function(e) {
  const foodItem = foodMain.options[foodMain.selectedIndex].value;
  const costumerName = costumerEl.options[costumerEl.selectedIndex].value;
  const costumer = costumer.find(({ name }) => name === costumerName);

  if (costomer && itemItem) {
    waiter.order.push(itemItem);
    console.log(costumers);
  };
  return false; // prevents redirect/refresh
};

The expected result would be for 'Timo' to order a mouse and keyboard and to add both price arguments in the order array to give a total.

ex.

(2) [Waiter, Waiter]
0: Waiter
name: "Timo"
order: Array(2)
0: "Keyboard (14.5)"
1: "Mouse (10)"
length: 2
__proto__: Array(0)
total: undefined
__proto__: Object

I want to calculate the total of the items that 'Timo' has ordered.

Your question has a lot of problems but i can help you with some of them.

First, you should change Costumer and Item to classes.

//Waiter Constructor
function Costumer(name, order, total){
    this.name = name;
    this.order = [];
    this.total = total;
}

to

//Waiter Constructor
class Costumer {
    constructor(name, order, total){
        this.name = name;
        this.order = [];
        this.total = total;
    }
}

the same goes to Item

//Item constuctor
class Item {
    constructor(item, price) {
        this.item = item;
        this.price = price;
    }
}

and then you can calculate the total price of 'Timo' items like this: (assuming that 'Timo' will be the first on the customers Array)

var totalPrice = 0;
for(let i = 0; i < costumer[0].order.length; ++i) {
    totalPrice += costumer[0].order[i].price;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM