繁体   English   中英

在嵌套的对象数组中按属性路径查找对象

[英]Find object by property path in nested array of objects

我有这样的对象:

var obj = [
    {
      name: 'ob_1',
      childFields: [],
    },
    {
      name: 'ob_2',
      childFields: [
        {
          name: 'ob_2_1',
          childFields: [
            {
              name: 'ob_3_1',
              childFields: [],
              test: 124
            },
          ],
        },
      ],
    },
  ]

function getObjectByNamePath(path, fieds) {
    const pathArr = path.split('.');
    const result = fieds.find(field => {
      if (pathArr.length > 1) {
        if (field.name === pathArr[0] && field.childFields.length) {
          const newPath = pathArr.slice(1, pathArr.length).join('.');
          return getObjectByNamePath(newPath, field.childFields);
        }
        return false;
      } else {
        if (field.name === pathArr[0]) {
            return true;
        } else {
            return false;
        }
      }
    });
    return result;
  }

我想通过名称值路径获取对象:

console.log(getObjectByNamePath('ob_2.ob_2_1.ob_3_1', obj))

我尝试过这个 ,但它不正确,我觉得有更优雅的方式来实现我想要的。 谢谢。

您可以迭代childFields并找到此级别的name ,然后使用下一级别名称。

 function getObjectByNamePath(path, array) { return path .split('.') .reduce( ({ childFields = [] } = {}, name) => childFields.find(o => o.name === name), { childFields: array } ); } var obj = [{ name: 'ob_1', childFields: [], }, { name: 'ob_2', childFields: [ { name: 'ob_2_1', childFields: [ { name: 'ob_3_1', childFields: [], test: 124 }] }] }]; console.log(getObjectByNamePath('ob_2.ob_2_1.ob_3_1', obj)); console.log(getObjectByNamePath('ob_1', obj)); console.log(getObjectByNamePath('foo.bar', obj)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

递归解决方案。

 var obj = [{ name: 'ob_1', childFields: [], }, { name: 'ob_2', childFields: [ { name: 'ob_2_1', childFields: [ { name: 'ob_3_1', childFields: [], test: 124 }] }] }] function getObjectByNamePath(path, obj) { const [currentPath, ...restPaths] = path.split('.'); const nextObj = (obj.find(e => e.name === currentPath)) if(!restPaths.length) return nextObj; return getObjectByNamePath(restPaths.join('.'), nextObj.childFields || []); } // Test Cases console.log(getObjectByNamePath('ob_2.ob_2_1.ob_3_1', obj)) console.log(getObjectByNamePath('ob_2.ob_2_1.ob_3_1.fakePath', obj)) console.log(getObjectByNamePath('ob_1', obj)) console.log(getObjectByNamePath('', obj)) 

暂无
暂无

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

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