showing results for - "add object to localstorage"
Nohlan
17 Nov 2019
1var addItem = function (name, image, price) {
2    var oldItems = JSON.parse(localStorage.getItem('itemsArray')) || [];
3    
4    var newItem = {
5        'product-name': name,
6        'product-image': image,
7        'product-price': price
8    };
9    
10    oldItems.push(newItem);
11    
12    localStorage.setItem('itemsArray', JSON.stringify(oldItems));
13};
14
15console.log(JSON.parse(localStorage.getItem('itemsArray')));
16
17addItem('name1', 'image1', 'price1');
18
19console.log(JSON.parse(localStorage.getItem('itemsArray')));
20
21addItem('name2', 'image2', 'price2');
22
23console.log(JSON.parse(localStorage.getItem('itemsArray')));
24