Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将阵列添加到本地存储并检索阵列_Javascript_Local Storage - Fatal编程技术网

Javascript 将阵列添加到本地存储并检索阵列

Javascript 将阵列添加到本地存储并检索阵列,javascript,local-storage,Javascript,Local Storage,我正在处理本地存储,我发现了一个问题 我想像这样存储键的值。这些值是动态添加的,应该用“,”分隔。我应该通过比较值来检索它们 这是我的看法 key value user1 a,b,c,d,e.... 应动态添加值字段中的值。假设我有用户名字段并添加朋友。用户名应采用位置键,添加好友(动态)应采用值位置 first iteration: username

我正在处理本地存储,我发现了一个问题

我想像这样存储键的值。这些值是动态添加的,应该用“,”分隔。我应该通过比较值来检索它们

这是我的看法

key                                       value
user1                                   a,b,c,d,e....
应动态添加值字段中的值。假设我有用户名字段并添加朋友。用户名应采用位置键,添加好友(动态)应采用值位置

first iteration:
username _a________
add friend _hello_______

localstorage : key                   value
                a                     hello
second iteration:

username _____a___
add friend : __stact___

local storage : key            value
                a              hello , stact
third iteration : 
username _____a___
add friend : __hi___

local storage : key            value
                a              hello , stact,hi
如果要删除stact,我必须编写
localStorage.removeKey(a[1])

请帮帮我,因为我是这方面的新手 我使用了下面的一个答案来回答我的代码,但它不起作用。相反,它是抛出错误 我试过以下方法

var ls = (function () {
    var _key = function (username) {
        return username; 
    },
    _saveFriends = function (username, friends) {
        localStorage.setItem(ls._key(username), JSON.stringify(friends));
    };

    function getFriends(username) {
        return JSON.parse(localStorage.getItem(ls._key(username)) || '[]');
    }

    return {
        getFriends: getFriends,
        addFriend: function (username, friend) {
            var friends = getFriends(username);
            friends.push(friend);
            _saveFriends(friends);
        },
        removeFriend: function (username, friend) {
            var friends = getFriends(username);
            var index = friends.indexOf(friend);
            if (index >= 0) {
                friends.splice(index, 1);
            }
            _saveFriends(friends);
        }
    };
})();

function main()
{
ls.addFriend('revanth','aastha');
ls.addFriend('revanth','pobala');
ls.getFriends('revanth');
这里是错误 未捕获的TypeError:undefined不是函数,错误在此行中 返回JSON.parse(localStorage.getItem(ls._键(用户名))

函数附加项(用户,str){
//在localStorage中获取值并拆分为数组
var tempArr=localStorage.getItem(user).split(“,”);
//如果仅添加唯一项,请检查是否唯一

//if(tempArr.indexOf(str)以下是一些可以在全局名称空间上使用的函数,以便在任何框架中都可以轻松地执行此操作:

var ls = (function () {
    var _key = function (username) {
        return username; // in case you want to namespace the keys later
    },
    _saveFriends = function (username, friends) {
        localStorage.setItem(ls._key(username), JSON.stringify(friends));
    };

    function getFriends(username) {
        return JSON.parse(localStorage.getItem(ls._key(username)) || '[]');
    }

    return {
        getFriends: getFriends,
        addFriend: function (username, friend) {
            var friends = getFriends(username);
            friends.push(friend);
            _saveFriends(friends);
        },
        removeFriend: function (username, friend) {
            var friends = getFriends(username);
            var index = friends.indexOf(friend);
            if (index >= 0) {
                friends.splice(index, 1);
            }
            _saveFriends(friends);
        }
    };
})();

// Use like so: 
//   ls.getFriends(username); 
//   ls.addFriend(username, friendname); 
//   ls.removeFriend(username, friendname);

我认为保存时需要将其转换为JSON字符串,否则需要将其转换为JSONify并使用它们
var ls = (function () {
    var _key = function (username) {
        return username; // in case you want to namespace the keys later
    },
    _saveFriends = function (username, friends) {
        localStorage.setItem(ls._key(username), JSON.stringify(friends));
    };

    function getFriends(username) {
        return JSON.parse(localStorage.getItem(ls._key(username)) || '[]');
    }

    return {
        getFriends: getFriends,
        addFriend: function (username, friend) {
            var friends = getFriends(username);
            friends.push(friend);
            _saveFriends(friends);
        },
        removeFriend: function (username, friend) {
            var friends = getFriends(username);
            var index = friends.indexOf(friend);
            if (index >= 0) {
                friends.splice(index, 1);
            }
            _saveFriends(friends);
        }
    };
})();

// Use like so: 
//   ls.getFriends(username); 
//   ls.addFriend(username, friendname); 
//   ls.removeFriend(username, friendname);