【问题标题】:iterate over nested objects to find the key attribute and return that object- Javascript遍历嵌套对象以找到关键属性并返回该对象 - Javascript
【发布时间】:2020-12-20 02:09:03
【问题描述】:

我有一个父对象和多个子对象,结构如下:

parent: {
  child1: {
    "aaa": {values: [], type: "string", title: "AAA"},
    "bbb": {values: [], type: "string", title: "BBB"},
    "ccc": {values: [], type: "string", title: "CCC"},
    "ddd": {values: [], type: "string", title: "DDD"}
  },
  child2: {
    "eee": {values: [], type: "string", title: "EEE"},
    "fff": {values: [], type: "string", title: "FFF"},
    "ggg": {values: [], type: "string", title: "GGG"},
    "hhh": {values: [], type: "string", title: "HHH"}
  },
  child3: {
    "iii": {values: [], type: "string", title: "III"},
    "jjj": {values: [], type: "string", title: "JJJ"},
    "kkk": {values: [], type: "string", title: "KKK"},
    "lll": {values: [], type: "string", title: "LLL"}
  }
}

现在我有一个函数,它返回一个子值作为子对象的键。例如:

function childVal() {
  //do somehting 
  return "aaa";
}

现在根据这个值,我想获取整个子对象,例如:如果返回值是"bbb",我希望返回具有"bbb" 键的对象,在这种情况下是child1

以下值的输出:

"aaa": return child1;
"ggg": return child2;
"bbb": return child1;
"lll": return child3;

这可能吗?

【问题讨论】:

  • 你没有关闭"lll": {values: [], type: "string", title: "LLL"}末尾的双引号。

标签: javascript object filtering


【解决方案1】:

function parentFilter(parent, myFilter) {
    return Object.values(parent).filter(child => Object.keys(child).some(key => key===myFilter));
}

let parent = {
       child1: {
      "aaa": {values: [], type: "string", title: "AAA"},
      "bbb": {values: [], type: "string", title: "BBB"},
      "ccc": {values: [], type: "string", title: "CCC"},
     "ddd": {values: [], type: "string", title: "DDD"}
    },
      child2: {
    "eee": {values: [], type: "string", title: "EEE"},
      "fff": {values: [], type: "string", title: "FFF"},
      "ggg": {values: [], type: "string", title: "GGG"},
     "hhh": {values: [], type: "string", title: "HHH"}
    },
      child3: {
    "iii": {values: [], type: "string", title: "III"},
      "jjj": {values: [], type: "string", title: "JJJ"},
      "kkk": {values: [], type: "string", title: "KKK"},
     "lll": {values: [], type: "string", title: "LLL"}
    }
 };
    
console.log(parentFilter(parent, 'aaa'));
console.log('-----------');
console.log(parentFilter(parent, 'ggg'));
console.log('-----------');
console.log(parentFilter(parent, 'bbb'));
console.log('-----------');
console.log(parentFilter(parent, 'lll'));

【讨论】:

    【解决方案2】:

    const parent = {
           child1: {
          "aaa": {values: [], type: "string", title: "AAA"},
          "bbb": {values: [], type: "string", title: "BBB"},
          "ccc": {values: [], type: "string", title: "CCC"},
         "ddd": {values: [], type: "string", title: "DDD"}
        }, 
            child2: {
        "eee": {values: [], type: "string", title: "EEE"},
          "fff": {values: [], type: "string", title: "FFF"},
          "ggg": {values: [], type: "string", title: "GGG"},
         "hhh": {values: [], type: "string", title: "HHH"}
        },
       }
    
    
    const val = 'aaa';  
    Object.keys(parent).forEach(el => {
      if(parent[el][val] !== undefined) {
        console.log(parent[el][val].values)
      }
    })

    【讨论】:

    • 这很棒,非常棒
    【解决方案3】:

    遍历所有属性,并检查给定键是否是该对象的属性之一。

    function childVal(key, parent) {
        return Object.values(parent).find(o => o.hasOwnProperty(key));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-07
      • 1970-01-01
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-19
      相关资源
      最近更新 更多