在Javascript递归中搜索各种数据的对象值



大家好!我在JavaScript上的搜索函数有问题。

这是我的一个对象(状态(:

{
"1": {
"id": "1",
"name": "Category #1",
"hasChild": "Y",
"count": "0",
"parentId": null,
"link": "/catalog/",
"subcategories": [
{
"id": "21",
"name": "Subcategory #1",
"hasChild": "Y",
"count": "0",
"parentId": "1",
"link": "/catalog/",
"subcategories": [
{
"id": "24",
"name": "subsubcategory #1",
"hasChild": "Y",
"count": "1",
"parentId": "21",
"link": "/catalog/",
"subcategories": [],
},
{
"id": "25",
"name": "subsubcategory #2",
"hasChild": "Y",
"count": "0",
"parentId": "21",
"link": "/catalog/",
"subcategories": [],
}
],
},
{
"id": "22",
"name": "Subcategory #2",
"hasChild": "Y",
"count": "0",
"parentId": "1",
"link": "/catalog/",
},
{
"id": "23",
"name": "Subcategory #3",
"hasChild": "Y",
"count": "0",
"parentId": "1",
"link": "/catalog/",
}
],
},
"2": {
"id": "2",
"name": "Category #2",
"hasChild": "Y",
"count": "0",
"parentId": null,
"link": "/catalog/",
"subcategories": [
..
],
},
}

我有一系列产品,其中一个产品具有其所属类别的id。所以我只从中提取了类别的唯一值。它可以是任何级别。

["24", "22", "2" ...]

我的目标是;name";父母类别的值。

示例:产品属于id为24的类别(名称:subsubcategory#1(。

我如何才能获得值"类别#1";来自顶级类别

我使用这个功能,但它只适用于第1级(如果id:1或2(

function filter(item, search, textKey) {
let result = []
const _filter = (item, search, textKey) => {
for (const i of item) {
if (i[textKey].indexOf(search) !== -1) {
result = [...result, { name: i.name, id: i.id, parentId: i.parentId }]
}
i.children ? _filter(i.children, search, textKey) : null
}
}
_filter(item, search, textKey)
return result
}
console.log(filter(Object.values(states), '24', 'id')) // didn't work
console.log(filter(Object.values(states), '2', 'id')) // found and mapped

我会构建一个函数,通过映射到一个查找与单个id相关联的名称的函数上,来查找与id列表相关联的名字。我会构建在一个基于任意谓词递归查找完整祖先节点的函数上。

您的初始输入不是一个完全递归的结构。看起来它可能是控制台输出的一个糟糕的副本,尽管它仍然可能是合法的。在任何情况下,我们首先进行转换,使用Object .values提取实际类别节点的数组。这可以从调用链的一个级别移动到另一个级别,这取决于您希望这些函数的重用级别。如果您的数据实际上是一个数组,这仍然有效,但我建议将Object .values (states)仅替换为states,因为这会使代码更干净。

const rootAncestor = (pred) => (xs) => 
xs .find (x => pred (x) || rootAncestor (pred) (x .subcategories || []))
const rootAncestorName = (states) => (id) => 
rootAncestor (x => x .id == id) (states) ?.name ?? ''
const rootAncestorNames = (states) => (ids) =>
ids .map (rootAncestorName (Object .values (states)))
const states = {1: {id: "1", name: "Category #1", hasChild: "Y", count: "0", parentId: null, link: "/catalog/", subcategories: [{id: "21", name: "Subcategory #1", hasChild: "Y", count: "0", parentId: "1", link: "/catalog/", subcategories: [{id: "24", name: "subsubcategory #1", hasChild: "Y", count: "1", parentId: "21", link: "/catalog/", subcategories: []}, {id: "25", name: "subsubcategory #2", hasChild: "Y", count: "0", parentId: "21", link: "/catalog/", subcategories: []}]}, {id: "22", name: "Subcategory #2", hasChild: "Y", count: "0", parentId: "1", link: "/catalog/"}, {id: "23", name: "Subcategory #3", hasChild: "Y", count: "0", parentId: "1", link: "/catalog/"}]}, 2: {id: "2", name: "Category #2", hasChild: "Y", count: "0", parentId: null, link: "/catalog/", subcategories: []}}
console .log (rootAncestorNames (states) (['24', '22', '2', '42']))

我用42添加了一个失败的查找,以表明我猜测我们想要返回一个空字符串。但在rootAncestorName结束时,您可以将其替换为nullundefined或其他令牌。

最新更新