简体   繁体   English

从req.query搜索对象内部

[英]Search inside an object from the req.query

I have a nested object displayed in my view and i want to search in it from the query like this : 我有一个嵌套的对象显示在我的视图中,我想从这样的查询中搜索它:

http://localhost:3000/test?$folder=folder

My object is like this : 我的对象是这样的:

   const test = {
        "item": [
          {
            "name": "folder",
            "item": [{
                "name": "subFolder",
                "item": [
                  {
                    "name": "subFolderTest1",
                    "item": [{
                      "path": "/",
                      "items": [{
                        "method": "GET",
                      }]
                    }]
                  },
                  {
                    "name": "subFolderTest2",
                    "item": [{
                      "path": "/",
                      "items": [{
                        "method": "GET",
                      }]
                    }]
                  }
                ]
            }]
        }
      ]
    }

If i have http://localhost:3000/test?$folder=folder then i would have : 如果我有http://localhost:3000/test?$folder=folder那么我会:

   {
        {
            "name": "subFolder",
            "item": [{},{}]
        }
   }

And if i have http://localhost:3000/test?$folder=folder/subFolder then i will have : 如果我有http://localhost:3000/test?$folder=folder/subFolder那么我将有:

{
          {
            "name": "subFolderTest1",
            "item": [{}]
          },
          {
            "name": "subFolderTest2",
            "item": [{}]
          }
    }

I know how to get what's inside my query and what the user's input but i don't how to search and display from something which is already displayed. 我知道如何获取查询中的内容以及用户的输入内容,但是我不知道如何从已经显示的内容中进行搜索和显示。

If you already have your query string you could split it by the "/" character. 如果您已有查询字符串,则可以用“ /”字符将其分隔。 Use the first name to find your first item, the second name to find the second, and so on. 使用名字来查找您的第一个项目,使用名字来查找第二个项目,依此类推。

var folder = "folder/subFolder";
var path = folder.split("/");
var currentItem = test["item"];

// Get each name you are looking for
path.forEach(function(name) {
    // get each entry of the current item
    currentItem.forEach(function(entry) {
        // if the name of the entry is the same as the 
        // name you are looking for then this is the 
        // next item you are looking for
        if (entry.name == name) {
            currentItem = entry["item"];
        }
    });
});

// now currentItem should be the entry specified by your path

You will still have to add code to handle situations like not having the name you are looking for in your current item, or having multiple entries with the correct name, and so on. 您仍然必须添加代码来处理以下情况,例如在当前项目中没有要查找的名称,或者具有正确名称的多个条目,等等。 I just kept it simple for the sake of clarity. 为了清楚起见,我只是保持简单。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM