I am creating a data structure for my project. It uses json. I am trying to make it easy to search or EDIT the json object. Which structure would be standard. Is there a right or wrong why for would it be okay for me to use either
object with id 2 has parent which is id 1
const items = [
{
id: 1,
title: 'First',
UUID: 123,
action : null,
isFolder: true,
parent: null,
},
{
id: 2,
title: 'Second',
UUID: 123,
action : null,
isFolder: false,
parent: 1,
},
{
id: 3,
title: 'Third',
UUID: 123,
action : null,
isFolder: false
parent: null,
},
]
structure 2
const items = [
{
id: 1,
title: 'First',
UUID: 123,
action : null,
isFolder: true,
children: [
{
id: 2,
title: 'Second',
UUID: 123,
action : null,
isFolder: false,
children: [],
},
]
},
{
id: 3,
title: 'Third',
UUID: 123,
action : null,
isFolder: false
children: [],
},
]
Option 1. Why? It's the simplest structure, it will be easy to edit individual nodes, to search, to be read by a human (imagine a model where your children have children have children and it gets hard to look at). Also, you should consider what has to happen if you want to change a parent? What if in the future you need to support multiple parents?
At the end of the day you could probably make arguments for either, so you need to choose what you think fits your application, use case, etc. Do you have any idea of what future enhancements might look like? Consider those.