0

I checked lots of documentation but couldnt find explanation for JSON.stringify([...mealIds,mealId]) . I understand we add mealId to the mealIds array . But can anybody send or share a link for this ? Another way to do this ?

function addMealToLocalStorage(mealId){    
    const mealIds = getMealsFromLocalStorage()
    localStorage.setItem('mealIds',JSON.stringify([...mealIds,mealId]))
}

function removeMealFromLocalStorage(mealId){
    const mealIds = getMealsFromLocalStorage();
    localStorage.setItem('mealIds',JSON.stringify(mealIds.filter(x => x!== mealId)))
}

function getMealsFromLocalStorage(){
    const mealIds = JSON.parse(localStorage.getItem('mealIds'))   
    return mealIds === null ? [] : mealIds
}

UndenCem
  • 11
  • 3
  • 1
    This is equivalent to `mealIds.push(mealId)` then saving the new `meailIds` array to local storage. It's just a little shorter. – Barmar Jan 21 '22 at 22:38

0 Answers0