ryoshpa
ryoshpa

Reputation: 433

How to append localstorage new object

I try to append new object inside localstorage. When i try to click to store new object, its not working. It's just changed existing value inside localstorage.

Here is my code

addLocalStorage(e) {
  let items = JSON.parse(localStorage.getItem('store')) || [];
  let obj = {}
  obj = {
    id: this.props.id,
    name: this.props.name,
    year: this.props.year
  };
  items.push(obj);
  localStorage.setItem('store ', JSON.stringify(items));
}

When I click to one of items my storage turns

[{id: "1", name: "lorem", year: "2016"}]

It's ok. But my problem is when ı clicked my other item it turns,

[{id: "2", name: "ipsum", year: "1990"}]

But i want to result

[{id: "1", name: "lorem", year: "2016"},{ id: "2", name: "ipsum", year: "1990" }]

How can i do that?

Upvotes: 0

Views: 45

Answers (1)

zerkms
zerkms

Reputation: 255155

You read and write different local storage items:

localStorage.setItem('store '

note the trailing space.

Upvotes: 1

Related Questions