我有一个嵌套对象的数组,动态键与此类似

const arr = [
      {
        "x1": [
          {
            "x1-point1": [
              {
                "value": 200,
                "angle": 20
              },
              {
                "value": 23,
                "angle": 90
              }
            ]
          },
          {
            "x1-point2": [
              {
                "value": 54,
                "angle": 0
              }
            ]
          },
          {
            "x1-point3": [
              {
                "value": 79
              }
            ]
          }
        ]
      },
      {
        "x2": [
          {
            "x2-point1": [
              {
                "value": 12
              }
            ]
          },
          {
            "x2-point2": [
              {
                "value": 24
              }
            ]
          },
          {
            "x2-point3": [
              {
                "value": 36
              }
            ]
          }
        ]
      }
    ]

我正在try 搜索该值,并获取父键和结果子键

val = 200
arr.filter(r => !!Object.values(r).find(t => t.value == val))

我的预期结果是

[
      {
        "x1": [
          {
            "x1-point1": [
              {
                "value": 200,
                "angle": 20
              }
            ]
          }
        ]
     }
]

我在这里做错了什么,我过滤与字符串匹配的最里面的子项并获取其父项

推荐答案

下面介绍的是实现预期目标的一种可能方法.

Code Snippet

// find needle "n" in hayStack "hs"
const myFind = (n, hs, res=[]) => {
  // search "hs" array for value "n"
  const foundIt = hs.find(ob => 'value' in ob && ob.value === n);
  
  // if found, add it to "res" array & return
  if (foundIt) {
    res.push(foundIt);
    return res;
  } else {
    // not-found - so, go to inner/nested level
    // hold result in "ires" - intermediate result array
    let ires = [];
    
    // for each object "ob" in "hs" array
    // iterate over "ob"'s key-value pairs
    // filter out pairs where "v" is not an array 
    hs.forEach(ob => {
      Object.entries(ob)
      .filter(([k, v]) => v && Array.isArray(v))
      .forEach(([k, v]) => {
        // make recursive call, this time "v" is the hayStack
        const ores = myFind(n, v);
        
        // if recursion result is present, accumulate it to "ires"
        if (ores && ores.length > 0) {
          ires.push({ [k] : ores });
        };
      })
    });
    
    // return if there is intermediate result
    if (ires.length > 0) return ires;
  };
  
  // value "n" not found
  return false;
};

const arr = [{
    "x1": [{
        "x1-point1": [{
            "value": 200,
            "angle": 20
          },
          {
            "value": 23,
            "angle": 90
          }
        ]
      },
      {
        "x1-point2": [{
          "value": 54,
          "angle": 0
        }]
      },
      {
        "x1-point3": [{
          "value": 79
        }]
      }
    ]
  },
  {
    "x2": [{
        "x2-point1": [{
          "value": 12
        }]
      },
      {
        "x2-point2": [{
          "value": 24
        }]
      },
      {
        "x2-point3": [{
          "value": 36
        }]
      }
    ]
  }
];

console.log('find value: 200 result: ', myFind(200, arr));
console.log('find value: 54 result: ', myFind(54, arr));
console.log('find value: 36 result: ', myFind(36, arr));
console.log('find value: 205 result: ', myFind(205, arr));
.as-console-wrapper { max-height: 100% !important; top: 0 }

Explanation

添加到上述代码段的内联注释.

PS:如果你想为stackoverflow社区增加价值,

请考虑阅读:What to do when my question is answered

Javascript相关问答推荐

使用ThreeJ渲染的形状具有抖动/模糊的边缘

TypeError:无法分解';React2.useContext(...)';的属性';basename';,因为它为空

从另一个数组中的对应行/键值对更新数组中的键值对对象

如何在我的Next.js项目中.blob()我的图像文件?

更新Redux存储中的对象数组

ReferenceError:无法在初始化之前访问setData

对不同目录中的Angular material 表列进行排序

$GTE的mongoose 问题

有角粘桌盒阴影

当达到高度限制时,如何裁剪图像?使用html和css

使用Java脚本替换字符串中的小文本格式hashtag

在Reaction Native中,ScrolltoIndex在结束时不一致地返回到索引0

根据网络速度加载CS

Zimjs库:如何让ZIM帧初始化日志(log)消失

不能用于addEventListener中的变量

如何用JAVASCRIPT更新背景位置值来实现CSS视差滚动?

如何在原生JavaScript中阻止精灵从画布上移开?

FromBody似乎没有处理C#API PUT调用,并且始终为空

JS函数返回未定义的值,即使返回值有效

如何在使用会话时防止争用情况?