在嵌套对象数组中通过属性路径查找对象

3

我有一个这样的对象:

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))

我尝试了这个,但它不能正常工作,我感觉有更优雅的方式来实现我的目标。谢谢。

2
可能是使用字符串键访问嵌套的JavaScript对象的重复问题。 - Mitya
@Utkanos,不完全是这样,因为这里涉及到了数组用于搜索,而不仅仅是对象。 - Nina Scholz
2个回答

5
你可以迭代 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; }


1
一个递归解决方案。

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))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接