简体   繁体   English

如何导航未知深度的嵌套对象?

[英]how to navigate nested objects of unknown depth?

Im making a notetaking app and Ive decided to store all the notes and structure in JSON file.我正在制作一个笔记应用程序,我决定将所有笔记和结构存储在 JSON 文件中。 On javascript, I get the JSON with AJAX, parse it and output it on the website.在 javascript 上,我得到了 JSON 和 AJAX,在网站上解析它和 output。

My note structure is array of objects that can be nested, like this (if it is a note, it has a "content" attribute, if it is a folder, it has an array of objects (can be empty array too if the folder should me empty):我的笔记结构是可以嵌套的对象数组,像这样(如果是笔记,它有一个“内容”属性,如果它是一个文件夹,它有一个对象数组(如果文件夹也可以是空数组我应该空着吗):

data {
  entries = [
    {
     name: "Some note",
     content: "This is a test note"
    },
    {
     name: "folder",
     children: [
       {
         name: "Bread recpie",
         content: "Mix flour with water..."
       },
       {
         name: "Soups",
         children: [
          {
            name: "Pork soup",
            content: "Add meat, onion..."
          },
          {
            name: "Chicken soup"
            content: "....."
          }
         ] 
       }
     ]
    }
  ]
}

To list the root directory, its simple, i just loop through the array as it only outputs the top-level records:要列出根目录,很简单,我只是循环遍历数组,因为它只输出顶级记录:

for (entry of data.entries) {
    const li = document.createElement("li");
    li.textContent = entry.name;
    if (entry.children) {
        li.className = "folder";
    } else {
        li.className = "file";
    }
    loop.appendChild(li); 
} 

But what about the folders?但是文件夹呢? How should I proceed in listing the folders if the depth of nesting is unknown?如果嵌套深度未知,我应该如何继续列出文件夹? And how do I target the specific folder?我如何定位特定文件夹? Should I add unique IDs to every object so i can filter the array with them?我是否应该为每个 object 添加唯一 ID,以便我可以用它们过滤数组? Or should I store some kind of depth information in a variable all the time?或者我应该一直在变量中存储某种深度信息吗?

You're making this more difficult for yourself by saving data to a JSON file.通过将数据保存到 JSON 文件,您正在使自己变得更加困难。 That is not a good approach.这不是一个好方法。 What you need to do is design a database schema appropriate for your data and create an API that outputs a predictable pattern of data that your client can work with.您需要做的是设计一个适合您的数据的数据库模式,并创建一个 API 输出您的客户可以使用的可预测数据模式。

I would suggest having a Folder resource and a Note resource linked through a one-to-many relationship.我建议通过一对多关系链接一个Folder资源和一个Note资源。 Each Folder resource can have many associated Note entries, but each Note has only one Folder that it is linked to.每个Folder资源可以有许多关联的Note条目,但每个Note只有一个链接到的Folder I suggest using an ORM, because most make it easy to eager load related data.我建议使用 ORM,因为大多数都可以轻松加载相关数据。 For instance, if you choose Laravel you can use Eloquent, and then getting all notes for a folder is as easy as:例如,如果您选择 Laravel,您可以使用 Eloquent,然后获取文件夹的所有笔记就像:

$folderWithNotes = Folder::with('notes')->where('name', 'school-notes')->get();

Knowing PHP is beside the point.知道PHP是题外话。 You should still be able to see the logic of that.你应该仍然能够看到其中的逻辑。

If you create a database and build a server-side API to handle your data, you will end up with JSON on your client side that has a predictable format and is easy to work with.如果您创建一个数据库并构建一个服务器端 API 来处理您的数据,您最终将在您的客户端得到 JSON,它具有可预测的格式并且易于使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在未知深度的物体中寻找价值? - How to find value within objects of unknown depth? 如何计算嵌套对象的深度级别? - How to count a depth level of nested objects? 深度未知时如何获取嵌套的Javascript对象? - How to get nested Javascript object when depth is unknown? 如何在 javascript 中的未知深度对象数组中找到特定属性值的最大深度? - How do I find the maximum depth of a particular property value in an array of objects of unknown depth in javascript? 从深度未知的嵌套数组中删除项目 - Removing items from a nested array with unknown depth 如何遍历树状未知深度的嵌套数据结构以查找和收集可寻址数组项? - How to traverse through a tree like nested data structure of unknown depth in order to find and collect addressable array items? 如何以最有效的方式完全调平(映射/减少/递归)未知深度的嵌套对象? - How to entirely level (map / reduce / recursion) a nested object of unknown depth in the most efficient manner? 如何在嵌套的 JSON 中导航 - How to navigate in nested JSON Javascript 中的嵌套对象是否有最大深度级别? - Is there a maximum depth level for nested objects in Javascript? 如何调用setState与嵌套对象和未知键进行反应 - How to call setState in react with nested objects and unknown key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM