Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda