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

时间:2018-05-30 13:18:02

标签: javascript

我有这样的对象:

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

我尝试了this,但它没有正常工作,我觉得有更优雅的方式来实现我想要的。感谢。

2 个答案:

答案 0 :(得分: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 :(得分: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))