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); } }); }Cuando agrego elementos por primera vez, funciona, pero si agrego elementos duplicados, continúa insertando elementos en mi Array.
En última instancia, estoy tratando de evitar que se agreguen elementos duplicados y actualizar la cantidad si existe en mi matriz.
Ingredientes es un modelo que tiene 2 propiedades (nombre, cantidad)
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; } }