Updating Variables/Arrays via Looping

Hi All!

I’m trying to figure out the below challenge. I need to update the amount of all dairy purchases to 25. Imagine I can do this via looping to update the array - any thoughts, advice? I feel like the solution is likely so simple, but I’m just stuck. Maybe I need some sleep.

class Food {
  constructor(type, name) {
    this.type = type;
    this.name = name;
  }
  
  get type() {
    return this.type;
  }
  
  get name() {
    return this.name;
  }
}

class Purchase {
  constructor(amount, food) {
    this.product = product;
    this.amount = amount;
  }

  get amount() {
    return this.amount;
  }

  setAmount(amount) {
    this.amount = amount;
  }
  
  get product() {
    return this.product;
  }
  
}

const creamFood = new Food("dairy", "Cream");
const baconFood = new Food("meat", "Bacon");
const yogurtFood = new Food("dairy", "Yogurt");
const milkFood = new Food("dairy", "Milk");
var purchase = [ new Purchase(2, cream_food), new Purchase(1, yogurt_food), new Purchase(12, bacon_food), new Purchase(15, cream_food) ];

First of all, you need to fix couple syntax issues:

  1. If you include get property_name() you also need to specify set property_name(), otherwise this won’t work:
this.type = type;
this.name = name;
  1. In Purchase class, this.product = product; - what’s a product?
  2. When invoking Purchase class on the last line, new Purchase(2, cream_food) - what’s the cream_food?

After it’s done, I suggest having a look at .map() method for Arrays:

Thank you for your help! I completed your suggestions. I’m a little concerned I didn’t complete the update correctly (dairy purchases to 25). I utilized your map suggestion. Is there perhaps a better way utilizing the .map() method and looping? Advice is greatly appreciated.

const newpurchase = 25;
const newData = original.map(obj => {
	const newObj = Object.assign({}, obj);
	if (newObj.type === 'dairy') newObj.payments = newpurchase;
	return newObj;
});