【问题标题】:Map return returning last iteration of recursion?映射返回返回递归的最后一次迭代?
【发布时间】:2018-11-19 13:42:41
【问题描述】:

我正在使用递归来遍历这个对象的子数组。然而,它正在返回顶级父母。对象数组是:

const orgs = {
   children:[{name:'Core Enginerinng Ops', orgId:741,
             children:[{name:'Child Engineering Ops', orgId:5656, 
                children:[{name: 'Child Engineering Last LEVEL AHAHHH', orgid:6969}]},{name: 'Child 2 Engineering OPS', orgId: 852}]},{name: 'Data Services Engineering', orgId: 456,
             children:[{name:'Child Data Services', orgId:978},{name: 'Child 2 Data Services', orgId: 354}]}]
    }

我的最终目标是将对象保存到一个新数组中,其中只有名称和 orgId 作为每个父子对象的对象。

flattenOrgs = (organizations) => {
  const flatArray =organizations.map(org => {
    if (org.children && org.children.length > 0) {
      this.flattenOrgs(org.children)
    }
    console.log(org.name)
    return org.name
  })
  return flatArray
}

但是,当我通过这个使用递归的函数传递它时,它只返回“org.name”:["Core Enginerinng Ops", "Data Services Engineering"]。我不擅长递归,但console.log(org.name) 按预期打印出每个人的名字对我来说没有意义......但它不返回那个名字?

编辑 console.log(org.name)返回前

儿童工程最后一级啊啊啊啊

儿童工程操作

Child 2 工程 OPS

核心工程操作

儿童数据服务

儿童 2 数据服务

数据服务工程

【问题讨论】:

    标签: javascript


    【解决方案1】:

    您可以减少数组而不是映射,因为您也需要子元素。

    const
        orgs = { children: [{ name: 'Core Enginerinng Ops', orgId: 741, children: [{ name: 'Child Engineering Ops', orgId: 5656, children: [{ name: 'Child Engineering Last LEVEL AHAHHH', orgid: 6969 }] }, { name: 'Child 2 Engineering OPS', orgId: 852 }] }, { name: 'Data Services Engineering', orgId: 456, children: [{ name: 'Child Data Services', orgId: 978 }, { name: 'Child 2 Data Services', orgId: 354 }] }] },
        flattenOrgs = (organizations) =>
            organizations.reduce((r, { name, orgId, children }) => 
                r.concat({ name, orgId }, flattenOrgs(children || [])), []);
    
    console.log(flattenOrgs(orgs.children));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • Nina....我觉得你刚刚向我展示了 .reduce 的作弊码。我以前从未听说过...谢谢
    【解决方案2】:

    我希望下面的代码能满足你的期望:

    const orgs = {
      children: [{
        name: 'Core Enginerinng Ops',
        orgId: 741,
        children: [{
          name: 'Child Engineering Ops',
          orgId: 5656,
          children: [{
            name: 'Child Engineering Last LEVEL AHAHHH',
            orgid: 6969
          }]
        }, {
          name: 'Child 2 Engineering OPS',
          orgId: 852
        }]
      }, {
        name: 'Data Services Engineering',
        orgId: 456,
        children: [{
          name: 'Child Data Services',
          orgId: 978
        }, {
          name: 'Child 2 Data Services',
          orgId: 354
        }]
      }]
    }
    
    console.log(orgs.children.map(child => {
      let newchild = {
        name: child.name,
        orgId: child.orgId
      }
      child.children && child.children.map(innerchild => {
        newchild.children = {
          name: innerchild.name,
          orgId: innerchild.orgId
        }
        innerchild.children && innerchild.children.map(innermostChild => {
          newchild.children.children = {
            name: innermostChild.name,
            orgId: innermostChild.orgId
          }
        })
      })
    return newchild
    }))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2016-05-26
      • 2021-08-19
      相关资源
      最近更新 更多