0

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.

punkrockbuddyholly
  • 9,675
  • 7
  • 36
  • 69
Triple -B
  • 261
  • 2
  • 12

1 Answers1

0

Save the data in LocalStorage as an array, or array of simple objects, but try to keep it DRY (don't repeat yourself). For example, if you have a list of people, keep an array of the people, but don't keep a separate variable for the count.

What you want is basically a "state". As long as you keep it minimal you will be fine. Then you can use lodash or underscore to find, merge, add, remove elements from that array.

Dan H
  • 3,524
  • 3
  • 35
  • 46