Home > Mobile >  How can I store data in a JavaScript Array so it can be stored in local storage?
How can I store data in a JavaScript Array so it can be stored in local storage?

Time:12-10

I'm trying to build a basic web store and I'm looking for a way to store data that I already have in an Excel sheet in a JavaScript array so that I can store it in the local storage if the buyer clicks on "Add to cart", for example: I have this data right here:

['Hoodie','Purple','cotton authentic',' Only £39.99','images/items/hoodies/hoodie (1).jpg']

['Hoodie','Light Blue','cotton authentic',' Only £39.99','images/items/hoodies/hoodie (2).jpg']

['Hoodie','Green','cotton authentic',' Only £39.99','images/items/hoodies/hoodie (3).jpg']

as you can see, it's the type of clothing, color, description, price and source of image

how can I store this in a JavaScript array so that I can create a function that upon clicking a button, adds the specific item in local storage?

CodePudding user response:

First off... from a data structure point of view, your solution is: interesting

['Hoodie','Purple','cotton authentic',' Only £39.99','images/items/hoodies/hoodie (1).jpg']

['Hoodie','Light Blue','cotton authentic',' Only £39.99','images/items/hoodies/hoodie (2).jpg']

['Hoodie','Green','cotton authentic',' Only £39.99','images/items/hoodies/hoodie (3).jpg']

Generally, in an array, you want to have multiple things that are all the same kind of thing. Yes, here each hoodie is represented as an array... and all the elements of each array are strings... but each string represents some entirely different type of thing.

Really, you should be using objects to store the hoodies:

{
    type: 'Hoodie',
    color: 'Purple',
    material: 'cotton authentic',
    price: '£39.99',
    image: 'images/items/hoodies/hoodie (1).jpg'
}

Now you have a single thing that has specific attributes each with a value

To show all of your hoodies, you have your array.. but now you load that array with those objects instead of having separate arrays

var hoodieArray = [{
    type: 'Hoodie',
    color: 'Purple',
    material: 'cotton authentic',
    price: '£39.99',
    image: 'images/items/hoodies/hoodie (1).jpg'
},
{
    type: 'Hoodie',
    color: 'Light Blue',
    material: 'cotton authentic',
    price: '£39.99',
    image: 'images/items/hoodies/hoodie (2).jpg'
},
{
    type: 'Hoodie',
    color: 'Green',
    material: 'cotton authentic',
    price: '£39.99',
    image: 'images/items/hoodies/hoodie (3).jpg'
}]

Now, you have a way to store, within a single key in the localStorage, the array of separate objects each with its list of attributes simply by passing in JSON.stringify(hoodieArray)

CodePudding user response:

Do something like this :

addToCart(item) {
        let items = getCartItem()
        items = items == null ? [] : items
        let found = items.find(x => x == item)
        if (found == null || undefined) {
            items.push(item)
            localStorage.setItem("mycart", JSON.stringify(items))
            return true;

        }
        return false;

    }

getCartItem() {
        let item = localStorage.getItem("mycart")
        item = item == null || undefined ? null : JSON.parse(item)
        return item;
    }

Use addToCart to add item to the cart and getCartItem to get the cart items

  • Related