Best format of localStorage

时间:2016-04-25 09:23:52

标签: javascript arrays object local-storage

I have array of object tree

var tree = [{
    "id": "1",
    "name": "one",
    "child": [],
    }, {
    "id": "2",
    "name": "two",
    "child": [{
        "id": "21",
        "name": "twentyOne",
        "child": [],
        },{
        "id": "22",
        "name": "twentyTwo",
        "child": [],
        }],
    }, {{
    "id": "3",
    "name": "three",
    "child": [],
    },
}].

Which one is the best way to store array of objects in localStorage? Is it better to use another format?

There are several methods:

  • getItem
  • getAllItem
  • removeItem
  • saveItem

But there are child arrays of objects. It means that I will use recursive search to find necessary object.

1 个答案:

答案 0 :(得分:0)

将LocalStorage中的数据保存为数组或简单对象数组,但请尽量保持 DRY (不要重复自己)。例如,如果您有一个人员列表,请保留一组人员,但不要为计数保留单独的变量。

你想要的基本上是一个状态"。只要你保持最小,你就可以了。然后,您可以使用lodashunderscore查找,合并,添加,删除该数组中的元素。