Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

89
Visualizações
my lastIndexOf is not working in my project
  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;
  }
}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda