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

231
Vistas
removing/check if there are duplicates from array and saving/updating the array dynamically in localstorage (JavaScript)

I have this function to push data into the array and save it in localstorage. My problem is that my function pushes to the array/saves to localstorage even though the same profileID exists. I want something that checks if the same profileId already exists remove the object and from localstorage/array then push to local storage/array. In short i do not want duplicates of the same profileID saved in localstorage or the array. I also want to keep the most new object instead of old and new saved in the array for the same profilID

 
  PUSHData(){
    var myobj = {
      Button: this.isReadMore,
       Class: this.sampleElem.className,
      ProfiliD: this.id,

    };
   
    
    const saved = JSON.parse(localStorage.getItem('CollapseState'));
    if(saved != null){
      this.array.push(myobj);
      localStorage.setItem('CollapseState',JSON.stringify(this.array));
    }else{
      this.array.push(myobj);
      localStorage.setItem('CollapseState',JSON.stringify(this.array));

    }

this is what localStorage looks like now

0: {Button: true, Class: "ShowHide", ProfiliD: "115279"}
1: [{Button: true, Class: "ShowHide", ProfiliD: "115279"},…]
  0: {Button: true, Class: "ShowHide", ProfiliD: "115279"}
  1: [{Button: true, Class: "ShowHide", ProfiliD: "115192"}]

I would really appreciatie any help

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

you should update existing item in array instead of push new one when same ProfiliD exists in localStorage:

PUSHData(){
    var myobj = {
      Button: this.isReadMore,
       Class: this.sampleElem.className,
      ProfiliD: this.id,

    };
   
    
    const saved = JSON.parse(localStorage.getItem('CollapseState'));
    if(saved != null){
      this.array[this.array.findIndex(x=>x.ProfiliD == myobj.ProfiliD)] = myobj;
      localStorage.setItem('CollapseState',JSON.stringify(this.array));
    }else{
      this.array.push(myobj);
      localStorage.setItem('CollapseState',JSON.stringify(this.array));

    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

As CollapseState is containing an array of objects in a localStorage. You can follow below steps to achieve this requirement :

  • Check if CollapseState array exist in localStorage or not (which you already doing)

    const saved = JSON.parse(localStorage.getItem('CollapseState'));
    
  • Now as saved will be an array, You can iterate over it to check if ProfiliD exist or not. If exist, splice/remove the object from the array.

    saved.forEach((o, index) => {
      if (o.ProfiliD === myobj.ProfiliD) {
        saved.splice(index, 1);
      }
    });
    
  • Now push the newObj into the saved array.

    saved.push(newObj);
    
  • Final step, set this updated saved array into the localStorage.

    localStorage.setItem('CollapseState',JSON.stringify(saved));
    
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