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

330
Vistas
How to update a specific value from the array in local storage?

There is an array of strings in the local storage. I want to find a specific value from that array, and update only that specific value without changing the rest of the array.

function saveLocalChecked(task) {
    let tasks = JSON.parse(localStorage.getItem('tasks'));
    const taskIndex = task.children[1].innerText;

    for(let i = 0; i < tasks.length; i++) {
        if(tasks[i] === taskIndex) {
            localStorage.setItem('tasks', JSON.stringify(tasks[i] + '*'));
        }
    }
}

So, in this function, I create the tasks array by pulling the items from the local storage. In the for loop, I loop over the tasks array to find a specific element. And now, I want to update that element by concatenating the asterisk sign. But after checking my local storage, there is only that updated element, and the rest of the array just disappeared.

How to fix this?

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

0

That is because you are replacing the entire storage for that specific array with the one single element you apply the change to. Change the item you want within your if condition and send the entire array, with the updated element, back to storage

function saveLocalChecked(task) {
    let tasks = JSON.parse(localStorage.getItem('tasks'));
    const taskIndex = task.children[1].innerText;

    for(let i = 0; i < tasks.length; i++) {
        if(tasks[i] === taskIndex) {
            tasks[i] = tasks[i] + '*'
            localStorage.setItem('tasks', JSON.stringify(tasks));
        }
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can always retrieve data from localstorage, modify the data and store it again. In your case, you can directly modify the array tasks before storing the modified array in local storage again.

function saveLocalChecked(task) {
    let tasks = JSON.parse(localStorage.getItem('tasks'));
    const taskIndex = task.children[1].innerText;

    //Modify elements
    for(let i = 0; i < tasks.length; i++) {
        if(tasks[i] === taskIndex) {
            tasks[i]+='*';
        }
    }
   
    //Store modified array in localstorage
    localStorage.setItem('tasks', JSON.stringify(tasks));
}
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