-1

I am trying to write my JSON array to localstorage, but it seems to overwrite the existing array

Here is my code

What am i doing wrong ?

var Basket = {
    ProductID: product,
    Quantity: quantity,
    Price: price
};

//STEP 2 - create an array 
var BasketContents = [];



//STEP 3 - create array of objects
BasketContents.push(Basket);

var count = BasketContents.length;

//   step 4 - reiterate through my array
for (i = 1; i <= BasketContents.length; i++) {
    BasketContents.push(Basket);
    localStorage.setItem('BasketContents', JSON.stringify(BasketContents[i]));
    if (i = BasketContents.length) {
        return;
    }
}


var lxs = JSON.parse(localStorage.getItem('BasketContents'));



console.log(lxs.length);

console.log(lxs);

The script above is triggered on button click, its a list of items that go into a shopping basket. So the BasketContents.push is triggered to increment values into the basket

chicken
  • 13
  • 4
  • 3
    The code as posted doesn't make much sense. After just one `.push()`, the length of the `BasketContents` array will *always* be 1. The loop will then push the *same object* one time, put it in local storage, and then (because the `if` statement uses `=` instead of `==`) *always* return from the function. – Pointy Sep 23 '19 at 14:04
  • 1
    Because you overwrite in each cylcle of your weird loop - why dont you just do `localStorage.setItem('BasketContents', JSON.stringify(BasketContents));` btw, there is only one entry in your basket? – Joschi Sep 23 '19 at 14:05
  • I think your writing inside the for loop and the local storage get and set Item using key only in that way it's overwrite i think so. – mariappan k Sep 23 '19 at 14:05
  • 2
    For future searchability, there's no such thing as a "JSON Array". JSON is a text format. You have an array. When you use `JSON.stringify` you convert the object at `BasketContents[i]` to a string to JSON format. Do note that you are converting and overwriting the localStorage item at "BasketContents" with that string on every iteration... – Heretic Monkey Sep 23 '19 at 14:05
  • `var count = BasketContents.length;` // This is going to equal 1 `BasketContents.length` // trying to find the length of an integer will always give you undefined. You might need to fix your loop condition, too. – Mohammed Mortaga Sep 23 '19 at 14:07
  • The script above is triggered on button click, its a list of items that go into a shopping basket. So the BasketContents.push is triggered to increment values into the basket – chicken Sep 23 '19 at 14:08
  • Please create a [mre] of how this code is used. As it is, if this code is run, it creates a new instance of `BasketContents` every time. You can use [Stack Snippets](https://meta.stackoverflow.com/q/358992/215552) to help. – Heretic Monkey Sep 23 '19 at 14:11
  • `if (i = BasketContents.length)` is a classic source of bugs, use `if (i == BasketContents.length)` as minimal fix, and read https://stackoverflow.com/questions/359494/which-equals-operator-vs-should-be-used-in-javascript-comparisons about why `===` is the even better pick. Also, that loop is unlikely needed at all, `JSON.stringify()` can deal with arrays in a single step. – tevemadar Sep 23 '19 at 14:15

1 Answers1

0

Just try to save whole array in local storage:

localStorage.setItem('BasketContents', JSON.stringify(BasketContents));