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

183
Visualizações
How to get object value from an array in the local storage and compare it with a new value?

I am trying to save an object into an array in the local storage,

but firstly I want to check either the object is already exist or not by comparing the existing Title in local storage with the new object's title. But I dont know how to do it.

const RecipeList = ({recipe}) => {


    const setData = () => {

      const newTitle = recipe.label;

        const existedFavRecipe = JSON.parse(localStorage.getItem('FavRecipes') || []);       

        // I want to write code that compare existedFavRecipe.Title with newTitle
        // if the title already exist it will give message and if not it will add the object into the local storage

            const NewFavRecipe = {
                Title : recipe.label,
                Image : recipe.image,
                Url : recipe.url,
                CuisineType : recipe.cuisineType,
                mealType : recipe.mealType,
                IngredientList: recipe.ingredients,
                Status : JSON.stringify(click)
            }
        existedFavRecipe.push(NewFavRecipe);
        localStorage.setItem('FavRecipes',JSON.stringify(existedFavRecipe));
    }
}

and the setData() will be triggered with onClick

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could do something like: const exists = existedFavRecipe.some((recipe) => recipe['Title'] == newTitle)

You will iterate over existedFavRecipe array to check if the value already exists

if(!exists) {
  const NewFavRecipe = {
                Title : recipe.label,
                Image : recipe.image,
                Url : recipe.url,
                CuisineType : recipe.cuisineType,
                mealType : recipe.mealType,
                IngredientList: recipe.ingredients,
                Status : JSON.stringify(click)
            }
        existedFavRecipe.push(NewFavRecipe);
        localStorage.setItem('FavRecipes',JSON.stringify(existedFavRecipe));
}

about 4 years ago · Juan Pablo Isaza Relatório
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