在javascript中搜索和更新嵌套对象

时间:2018-02-20 18:41:33

标签: javascript

我有一个嵌套对象,在搜索特定键时,我想更新原始对象。

var people= {
      "i": [
        {
          "country": "Australia",
          "list": [
            {
              "name": "ABC   ",
              "address": "AB street ",
            }
            ...
          ]
        },
        {
          "country": "Brazil",
          "list": [
            {
              "name": "XZ   ",
              "address": "AB street "
            },
           ...
          ]
        }
      ]
     ...
    };

我想按名称搜索

2 个答案:

答案 0 :(得分:1)

您需要使用函数inputfile <- "myinputfile" system.string <- paste0("plink --bfile ",inputfile," --freq") system(system.string) 找到现有对象。

&#13;
&#13;
find
&#13;
var pages = {    "1":[    {      "title":"Australia",      "list":[        {          "name":"ABC   ",          "label":"08RAH - S650 ",          "description":"S650 Combine HX"        },        {          "name":"ABC  ",          "label":"00CAH - S660 ",          "description":"S660 Combine HX"        }      ]    },   {      "title":"Brazil",      "list":[        {          "name":"XZ   ",          "label":"08RAH - S650 ",          "description":"S650 Combine HX"        },        {          "name":"W  ",          "label":"00CAH - S660 ",          "description":"S660 Combine HX"        }      ]    }  ],  "2":[    {      "title":"Australia",      "list":[        {          "name":"ABC   ",          "label":"01KBZ - S670 ",          "description":"S670 Small Grain HillMaster Combine ZX"        },        {          "name":"DE   ",          "label":"00CAH - S660 ",          "description":"S660 Combine HX"        }      ]    },    {      "title":"United Kingdom",      "list":[        {          "name":"ABC   ",          "label":"01KBZ - S670 ",          "description":"S670 Small Grain HillMaster Combine ZX"        },        {          "name":"ABC   ",          "label":"00CAH - S660 ",          "description":"S660 Combine HX"        }      ]    },  ],  "3":[    {      "title":"United States",      "list":[        {          "name":"ABC   ",          "label":"01KBZ - S670 ",          "description":"S670 Small Grain HillMaster Combine ZX"        },        {          "name":"DF   ",          "label":"01LAZ - S680 ",          "description":"S680 Small Grain Combine ZX"        }      ]    },    {      "title":"Australia",      "list":[        {          "name":"ABC   ",          "label":"01KBZ - S670 ",          "description":"S670 Small Grain HillMaster Combine ZX"        },        {          "name":"ABC  ",          "label":"01LAZ - S680 ",          "description":"S680 Small Grain Combine ZX"        },        {          "name":"ABC   ",          "label":"01NBZ - S680 ",          "description":"S680 HillMaster™ Combine ZX"        }      ]    }  ]};

var searchedText = "AB";
var filteredPages = {};

var allPages = getFilteredPages(pages, searchedText);

function getFilteredPages(pages, searchedText) {

  Object.keys(pages).map(function(pageNumber) {
    pages[pageNumber].map(function(pageItem, index) {
      pageItem.list.map(function(item) {
        if (item.name.toLowerCase().indexOf(searchedText.toLowerCase()) !== -1) {
          if (filteredPages[pageNumber]) {
            var p = filteredPages[pageNumber].find((p) => p.title === pageItem.title);
            
            if (p) {
              p.list.push(item);
            } else {
              filteredPages[pageNumber].push({
                title: pageItem.title,
                list: [item]
              });
            }
          } else  {
            filteredPages[pageNumber] = [{
              title: pageItem.title,
              list: [item]
            }];
          }
        }
      });
    });

  });
  return filteredPages;

}


console.log('allPages >>>>>', allPages);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

尝试(复制然后)使用delete / unshift运算符修改原始对象,而不是创建新对象filteredPages。这样会更容易思考,你会保持原有的结构。