Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建具有嵌套父对象的Javascript对象_Javascript_Node.js_Recursion_Adonis.js - Fatal编程技术网

创建具有嵌套父对象的Javascript对象

创建具有嵌套父对象的Javascript对象,javascript,node.js,recursion,adonis.js,Javascript,Node.js,Recursion,Adonis.js,我正在从事一个Nodejs项目。我必须创建一个函数,它接受一个对象(子类别),如: 现在,我希望我的函数使用数据库中的parent\u id检查父类别,并返回如下数组/对象: { id: 2, name: 'Furniture', parent: { id: 1, name: 'Residential', parent: { id: ..., name: ...,

我正在从事一个Nodejs项目。我必须创建一个函数,它接受一个对象(子类别),如:

现在,我希望我的函数使用数据库中的
parent\u id
检查父类别,并返回如下数组/对象:

{
    id: 2,
    name: 'Furniture',
    parent: {
        id: 1,
        name: 'Residential',
        parent: {
            id: ...,
            name: ...,
            parent: {
                and so on..
            }
        }
    }
}
const parents = yield this._get_category_parents(category)
[
    {
        "id": 2,
        "name": "Furniture"
    },
    {
        "id": 1,
        "name": "Residential"
    }
]
这就是我到目前为止所做的:

* _get_category_parents(category, _array) {

    if(_array === undefined) _array = []

    if( category.parent_id !== 0 ) {
      const c_parent = yield this.Database.from('categories').where('id', '=', category.parent_id)
      _array.push({id: c_parent[0].id, name: c_parent[0].name})
      yield this._get_category_parents(c_parent[0], _array)

    }

    return _array

  }
这样调用这个函数:

{
    id: 2,
    name: 'Furniture',
    parent: {
        id: 1,
        name: 'Residential',
        parent: {
            id: ...,
            name: ...,
            parent: {
                and so on..
            }
        }
    }
}
const parents = yield this._get_category_parents(category)
[
    {
        "id": 2,
        "name": "Furniture"
    },
    {
        "id": 1,
        "name": "Residential"
    }
]
这将返回一个类似以下内容的父级数组:

{
    id: 2,
    name: 'Furniture',
    parent: {
        id: 1,
        name: 'Residential',
        parent: {
            id: ...,
            name: ...,
            parent: {
                and so on..
            }
        }
    }
}
const parents = yield this._get_category_parents(category)
[
    {
        "id": 2,
        "name": "Furniture"
    },
    {
        "id": 1,
        "name": "Residential"
    }
]
我希望住宅对象附加到家具的父节点中


我在这上面花了太多时间,但没有得到我想要的。非常感谢您的帮助。

您需要考虑的是递归解决方案

因为您调用的是数据库,所以可能不太可能,但是如果按id查找是同步的,那么您可以使用如下代码(请注意,我在这里冒充db):

const getHierarchy=(查找,子项)=>{
const{id,name,parent_id}=lookup(child)|
{id:null,name:null,父\u id:0}
返回父项\u id==0
?{id,name,parent_id}
:{…{id,name},{parent:getHierarchy(查找,{parent_id})}
}
常数项=[
{id:1,姓名:'居住',家长id:5},
{id:2,名称:'Furniture',父\u id:1},
{id:3,名称:'Other',父\u id:0},
{id:4,名称:'FooBar',父\u id:3},
{id:5,名称:'Stuff',父\u id:0}
]
const lookup=child=>items.find(item=>item.id==child.parent\u id)
const item={id:65,name:'Outdoor',parent_id:2}

log(getHierarchy(lookup,item))
生成器的实现似乎很棘手。。。我相信使用Promissions或async/await会更容易。我认为在简单递归方面。@ali您使用的是哪种数据库?有些数据库支持递归查询,允许您使用单个查询获取所有数据,而不是使用javascript一次查询一条记录。@naomik我使用的是mysqlWow。。工作得很有魅力。正是我需要的。这是一个一次性解决方案。非常感谢Scott Sauyet。