创建 Javascript Object 嵌套 Parent Objects
Create Javascript Object Having Nested Parent Objects
我正在做一个 Nodejs 项目。我必须创建一个带有 object(child 类别)的函数,例如:
{
id: 65,
name: 'Outdoor',
parent_id: 2
}
现在我希望我的函数通过使用数据库中的 parent_id
和 return 一个 array/object 来检查 parent 类别,如下所示:
{
id: 2,
name: 'Furniture',
parent: {
id: 1,
name: 'Residential',
parent: {
id: ...,
name: ...,
parent: {
and so on..
}
}
}
}
这是我到目前为止所做的:
* _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
}
并像这样调用这个函数:
const parents = yield this._get_category_parents(category)
这个 return 是我的一个 parent 数组,如下所示:
[
{
"id": 2,
"name": "Furniture"
},
{
"id": 1,
"name": "Residential"
}
]
我希望将住宅 object 添加到家具的 parent 节点中。
我在这上面花了太多时间,但没有得到我想要的。任何帮助将不胜感激。
你想的是递归的解法
由于您调用的是数据库,这可能不太可能,但是如果按 id 进行的查找是同步的,您可以使用类似以下的代码来完成(请注意,我在这里伪造了一个数据库):
const getHierarchy = (lookup, child) => {
const {id, name, parent_id} = lookup(child) ||
{id: null, name: null, parent_id: 0}
return parent_id == 0
? {id, name, parent_id}
: {...{id, name}, ...{parent: getHierarchy(lookup, {parent_id})}}
}
const items = [
{id: 1, name: 'Residential', parent_id: 5},
{id: 2, name: 'Furniture', parent_id: 1},
{id: 3, name: 'Other', parent_id: 0},
{id: 4, name: 'FooBar', parent_id: 3},
{id: 5, name: 'Stuff', parent_id: 0}
]
const lookup = child => items.find(item => item.id == child.parent_id)
const item = {id: 65, name: 'Outdoor', parent_id: 2}
console.log(getHierarchy(lookup, item))
您必须编写适当的 lookup
函数,大概使用 this.Database.from(...)
。您可能还想要在 lookup
函数中烘焙的简化版本,在这种情况下,您可以编写
const getAncestry = (item) => getHierarchy(lookup, item)
如果您的查找更有可能是异步的,那么这将影响 getHierarchy
以及您如何调用它。这是一种可能性:
const getHierarchy = async (lookup, child) => {
const {id, name, parent_id} = await lookup(child) ||
{id: null, name: null, parent_id: 0}
return parent_id == 0
? {id, name, parent_id}
: {...{id, name}, ...{parent: await getHierarchy(lookup, {parent_id})}}
}
const items = [
{id: 1, name: 'Residential', parent_id: 5},
{id: 2, name: 'Furniture', parent_id: 1},
{id: 3, name: 'Other', parent_id: 0},
{id: 4, name: 'FooBar', parent_id: 3},
{id: 5, name: 'Stuff', parent_id: 0}
]
const lookup = async child => new Promise(
(resolve, reject) => setTimeout(
() => resolve(items.find(item => item.id == child.parent_id)),
1000
)
)
const getAncestry = async item => getHierarchy(lookup, item)
const item = {id: 65, name: 'Outdoor', parent_id: 2}
getAncestry(item).then(console.log)
请注意函数调用方式的变化。您需要对结果承诺调用 .then()
以获得任何有用的行为。
我正在做一个 Nodejs 项目。我必须创建一个带有 object(child 类别)的函数,例如:
{
id: 65,
name: 'Outdoor',
parent_id: 2
}
现在我希望我的函数通过使用数据库中的 parent_id
和 return 一个 array/object 来检查 parent 类别,如下所示:
{
id: 2,
name: 'Furniture',
parent: {
id: 1,
name: 'Residential',
parent: {
id: ...,
name: ...,
parent: {
and so on..
}
}
}
}
这是我到目前为止所做的:
* _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
}
并像这样调用这个函数:
const parents = yield this._get_category_parents(category)
这个 return 是我的一个 parent 数组,如下所示:
[
{
"id": 2,
"name": "Furniture"
},
{
"id": 1,
"name": "Residential"
}
]
我希望将住宅 object 添加到家具的 parent 节点中。
我在这上面花了太多时间,但没有得到我想要的。任何帮助将不胜感激。
你想的是递归的解法
由于您调用的是数据库,这可能不太可能,但是如果按 id 进行的查找是同步的,您可以使用类似以下的代码来完成(请注意,我在这里伪造了一个数据库):
const getHierarchy = (lookup, child) => {
const {id, name, parent_id} = lookup(child) ||
{id: null, name: null, parent_id: 0}
return parent_id == 0
? {id, name, parent_id}
: {...{id, name}, ...{parent: getHierarchy(lookup, {parent_id})}}
}
const items = [
{id: 1, name: 'Residential', parent_id: 5},
{id: 2, name: 'Furniture', parent_id: 1},
{id: 3, name: 'Other', parent_id: 0},
{id: 4, name: 'FooBar', parent_id: 3},
{id: 5, name: 'Stuff', parent_id: 0}
]
const lookup = child => items.find(item => item.id == child.parent_id)
const item = {id: 65, name: 'Outdoor', parent_id: 2}
console.log(getHierarchy(lookup, item))
您必须编写适当的 lookup
函数,大概使用 this.Database.from(...)
。您可能还想要在 lookup
函数中烘焙的简化版本,在这种情况下,您可以编写
const getAncestry = (item) => getHierarchy(lookup, item)
如果您的查找更有可能是异步的,那么这将影响 getHierarchy
以及您如何调用它。这是一种可能性:
const getHierarchy = async (lookup, child) => {
const {id, name, parent_id} = await lookup(child) ||
{id: null, name: null, parent_id: 0}
return parent_id == 0
? {id, name, parent_id}
: {...{id, name}, ...{parent: await getHierarchy(lookup, {parent_id})}}
}
const items = [
{id: 1, name: 'Residential', parent_id: 5},
{id: 2, name: 'Furniture', parent_id: 1},
{id: 3, name: 'Other', parent_id: 0},
{id: 4, name: 'FooBar', parent_id: 3},
{id: 5, name: 'Stuff', parent_id: 0}
]
const lookup = async child => new Promise(
(resolve, reject) => setTimeout(
() => resolve(items.find(item => item.id == child.parent_id)),
1000
)
)
const getAncestry = async item => getHierarchy(lookup, item)
const item = {id: 65, name: 'Outdoor', parent_id: 2}
getAncestry(item).then(console.log)
请注意函数调用方式的变化。您需要对结果承诺调用 .then()
以获得任何有用的行为。