【问题标题】:Retrieve nested object by key按键检索嵌套对象
【发布时间】:2017-09-27 01:34:34
【问题描述】:

在以下对象中通过其键 (itemID) 检索嵌套对象的最有效方法是什么(考虑到性能,假设对象可能很大)?

显然,我可以通过首先引用每个父项 (searchResults.page2.item3) 来访问该对象,但是如果我不知道它在哪个页面上,我如何仅使用 itemID 从 searchResults 中检索 item3?

var searchResults = {

    page1 : {

        item1 : {},
        item2 : {}      

    },

    page2 : {

        item3 : {},
        item4 : {}  
    }

}

【问题讨论】:

  • 您直接从“最有效的方式”开始,请添加您的方式。

标签: javascript object search nested return


【解决方案1】:

只需循环遍历您的对象。这是一个有效的解决方案。希望对您有所帮助!

var searchResults = {
        page1 : {

            item1 : {},
            item2 : {}

        },
        page2 : {

            item3 : { id: 3},
            item4 : {}
        }
    }

    for(var i in searchResults){
        var pageNumber = searchResults[i];
        for(var j in pageNumber){
        var deeperProperty = pageNumber[j];
            if(deeperProperty.hasOwnProperty("id") && deeperProperty.id === 3){
                console.log(deeperProperty);
            }
        }
    }

【讨论】:

    【解决方案2】:

    使用递归函数检查整个对象,直到找到 item3

    var searchResults = {
      page1: {
        item1: {
          id: 1
        },
        item2: {
          id: 2
        }
      },
      page2: {
        item3: {
          id: 3
        },
        item4: {
          id: 4
        }
      }
    }
    
    /* start the operation */
    recurse_object(searchResults, 0);
    
    function recurse_object(obj, curIdx) {
      /* create array with all keys for an object */
      var keys = Object.keys(obj);
    
    
      /* check if we have gone through all of the object's keys */
      if (curIdx < keys.length) {
    
        /* if not, grab the key associated with curIdx */
        var keyVal = keys[curIdx];
    
        if (keyVal == 'item3') {
          console.log("FOUND ITEM3 ID! " + obj.item3.id);
        } else {
          /* check if the current item has any sub-keys to check  */
          if (Object.keys(obj[keyVal]).length > 0) {
    
    
            /* check the any keys that exist in a particular key */
            recurse_object(obj[keyVal], 0);
          }
          /* now increase the index to check for the next key on object */
          curIdx++;
          recurse_object(obj, curIdx);
        }
      }
    
    }

    编辑:

    具有多个包含 item3 的输入对象以及其他嵌套级别的未注释版本

    var searchResults = {
      page1: {
        item1: {
          id: 1
        },
        item2: {
          id: 2
        }
      },
      page2: {
        item3: {
          id: 3
        },
        item4: {
          id: 4
        }
      },
      page20: {
        subPage1: {
          item3: {
            id: 6
          }
        },
        item5: {
          subItem2: {
            deepItem1: {
              item3: {
                id: 10
              }
            }
          }
        }
      }
    }
    
    
    recurse_object(searchResults, 0);
    
    function recurse_object(obj, curIdx) {
    
      var keys = Object.keys(obj);
    
    
    
      if (curIdx < keys.length) {
    
    
        var keyVal = keys[curIdx];
    
        if (keyVal == 'item3') {
          console.log("FOUND ITEM3 ID! " + obj.item3.id);
        } else {
    
          if (Object.keys(obj[keyVal]).length > 0) {
    
    
    
            recurse_object(obj[keyVal], 0);
          }
    
          curIdx++;
          recurse_object(obj, curIdx);
        }
      }
    
    }

    【讨论】:

      【解决方案3】:

      这似乎一点也不高效,但它可以满足您的要求,因为“如果我不知道它在哪个页面上,我可以只使用 itemID 从 searchResults 中检索 item3 吗?”去。

      var searchResults = {
        page1: {
          item1: { id: 1 },
          item2: { id: 2 }
        },
        page2: {
          item3: { id: 3 },
          item4: { id: 4 }
        }
      }
      
      var innerObjects = Object
      .keys(searchResults)
        .map(
          function(key, index) {
            return Object
              .keys(searchResults[key])
                .map(
                  function(innerKey, index) {
                    return searchResults[key][innerKey];
                  }
                );
          });
      //console.log(innerObjects);
      var flattenedArray = [].concat.apply([], innerObjects);
      //console.log(flattenedArray);
      var idYoureLookingFor = 3;
      var objectYouWant = flattenedArray.filter(function(obj) { return obj.id == idYoureLookingFor; });
      console.log(objectYouWant);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-09
        • 1970-01-01
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 2017-05-14
        • 1970-01-01
        • 2017-11-19
        相关资源
        最近更新 更多