0
  addIngredients(ingredients: Ingredient[] | undefined) {
    ingredients?.filter(item => {
      // @ts-ignore
      console.log(this.ingredients.lastIndexOf(item.name));
      // @ts-ignore
      if(this.ingredients.lastIndexOf(item.name.toString()) === -1)
      {
        this.ingredients.push(item);
      }
    });
}

When I add items to my first time, it works but if I add duplicate items, it continues to push items in my Array.

Ultimately I am trying to prevent duplicate items from being added and update the amount if it does exist in my array.

Ingredients is a model which has 2 properties (name, amount)

export class Ingredient {
  constructor(public name: string, public amount: number) {
  }
}


export class Recipe {
  public name!: string;
  public description!: string;
  public imagePath!: string;
  public ingredients: Ingredient[] = [];

  constructor(name: string, description: string, imagePath: string, ingredients: Ingredient[]) {
    this.name = name;
    this.description = description;
    this.imagePath = imagePath;
    this.ingredients = ingredients;
  }
}
software is fun
  • 7,286
  • 18
  • 71
  • 129
  • `@ts-ignore` defeats the purpose of TypeScript - type things properly and you'll be able to spot errors more easily. Your array is an array of objects, not an array of strings, so `lastIndexOf` (or `indexOf`) won't work – CertainPerformance Jun 02 '22 at 17:30
  • what would I need to do to compare the name element of the array ingredient? – software is fun Jun 02 '22 at 17:34
  • Did you check out the linked canonical? – CertainPerformance Jun 02 '22 at 17:35
  • I did but I don't know the ID of the item in the array. The Front End submits an ingredient like 'Meat' and the quantity (like 3) and it gets saved to this array. I am trying to improve this by preventing duplicates from being added but just updating the quantity – software is fun Jun 02 '22 at 17:39
  • Sure, the property is named `name`, not `id`, but that should be trivial to tweak... – CertainPerformance Jun 02 '22 at 17:40
  • I've seen solutions like this but const res = myArray.find(x => x.id === '100')?.foo; // No error! This example shows x.Id being compared to a static value (100). I need to compare it with another array. – software is fun Jun 02 '22 at 17:49
  • You don't need to compare it with another array, you need to compare it against the *one name string* in question to see if it's a duplicate item - so use that string – CertainPerformance Jun 02 '22 at 17:52
  • my addIngredients method accepts an array as input. This array length could have items for a recipe for a burger (patty, ketchup, mayo, lettuce, etc). I am comparing it to the shopping list ingredients (which contains all the ingredients for all the recipes that I am trying to make) so from a previous transaction, I could have my famous fries (which is potatos, ketchup, salt, pepper). As you can see the burger requires ketchup as well. I don't want to list Ketchup twice but I'd rather increment the amount – software is fun Jun 02 '22 at 17:55
  • Which boils down to finding an element by ID (or name) in an array of objects... – CertainPerformance Jun 02 '22 at 17:58

0 Answers0