Click favorites, how to push the array data on localStorage

problem description

1. Click on the collection to cache the current product locally, and the backend has put the product data in the header. I will just call it directly.

I clicked on different items four times, but I didn"t need to append them to the array. Could you tell me how to append the items clicked later in the code to the same array

clipboard.png

        Wishlist:function () {


            var array = [];
            let adrObj = {
                "id": misslove.product.info.id,
                "name":misslove.product.info.name,
                "image":misslove.product.info.image,
                "href":misslove.product.info.href,
            };
            array[0]= adrObj;

            store.set(misslove.product.info.id,array); //  -  JSON.stringify

/ /

            var user = store.get(misslove.product.info.id,array); //  -  JSON.parse

            console.log(user);
            store.forEach(function(key, array) {
                console.log( array)
            })  // 


        },
Nov.10,2021

try this (the code is so long):

/*
localstorage
*/
var mystorage = (function mystorage(){
    var ms = "mystorage";
    var storage=window.localStorage;
    if(!window.localStorage){
        alert("localstorage");
        return false;
    }

    var set = function(key,value){
        //
        var mydata = storage.getItem(ms);
        if(!mydata){
            this.init();
            mydata = storage.getItem(ms);
        }
        mydata = JSON.parse(mydata);
        mydata.data[key] = value;
        storage.setItem(ms,JSON.stringify(mydata));
        return mydata.data;

    };

    var get = function(key){
        //
        var mydata = storage.getItem(ms);
        if(!mydata){
            return false;
        }
        mydata = JSON.parse(mydata);

        return mydata.data[key];
    };

    var remove = function(key){
        //
        var mydata = storage.getItem(ms);
        if(!mydata){
            return false;
        }

        mydata = JSON.parse(mydata);
        delete mydata.data[key];
        storage.setItem(ms,JSON.stringify(mydata));
        return mydata.data;
    };

    var clear = function(){
        //
        storage.removeItem(ms);
    };

    var init = function(){
        storage.setItem(ms,'{"data":{}}');
    };

    return {
        set : set,
        get : get,
        remove : remove,
        init : init,
        clear : clear
    };



})();

mystorage.set('d', [1,[2,3]]);
mystorage.get('d'); // [1,[2,3]]


--------------------- 
:8 
:CSDN 
:https://blog.csdn.net/guo8ke/article/details/70846743 
:
< hr >

the idea is to use the mystorage method to read and manipulate the array;

execute the mystorage.set method again after the operation (push new data) is finished.

Menu