今天是個好日子!我對 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" ...]
我的目標是采用父母類別的“名稱”值。
示例:產品屬于 id:24 的類別(名稱:subsubcategory #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
uj5u.com熱心網友回復:
我將構建一個函式,通過映射查找與單個 id 關聯的名稱的函式來查找與 id 串列關聯的名稱。我將在一個函式之上構建,該函式基于任意謂詞遞回地找到完整的祖先節點。
您的初始輸入不是一個遞回結構。看起來它可能是來自控制臺輸出的錯誤副本,盡管它仍然可能是合法的。無論如何,我們首先進行轉換,Object .values用于提取實際類別節點的陣列。這可以從呼叫鏈的一個級別移動到另一個級別,具體取決于您希望從這些函式中重用的級別。如果您的資料實際上是一個陣列,這仍然可以作業,但我建議用Object .values (states)just替換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,您可以將其替換為null,undefined或其他一些標記。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/516790.html
上一篇:我在角度中遇到“遞回過多”錯誤
下一篇:遞回函式回傳奇怪的輸出?
