gpt4 book ai didi

javascript - 如何在深层嵌套数组中查找具有 id 值的对象?

转载 作者:行者123 更新时间:2023-11-30 07:20:23 27 4
gpt4 key购买 nike

给定这个结构,我如何在这个深度嵌套的对象结构中找到具有给定 id 的对象。

const menuItems = [
{
id: 1,
imageUrl: "http://placehold.it/65x65",
display: "Shop Women",
link: "#",
type: "image",
nextItems: [
{
id: 10,
display: "홈",
link: "#",
type: "menuitem"
},
{
id: 20,
display: "의류",
link: "#",
type: "menuitem-withmore",
nextItems: [
{
id: 100,
display: "I'm inside one nest",
link: "#",
type: "menuitem"
}
]
},
{
id: 30,
display: "가방",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
id: 40,
display: "신발",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
id: 50,
display: "악세서리",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
id: 60,
display: "SALE",
link: "#",
type: "menuitem-withmore",
style: "bold",
nextItems: []
},
{
id: 70,
display: "브랜드",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
type: "separator"
},
{
id: 80,
display: "위시리스트",
link: "#",
type: "menuitem"
},
{
id: 90,
display: "고객센터",
link: "#",
type: "menuitem"
},
{
id: 99,
display: "앱 다운로드",
link: "#",
type: "menuitem"
}
]
},
{
id: 2,
imageUrl: "http://placehold.it/65x65",
display: "Shop Men",
link: "#",
type: "image",
nextItems: [
{
id: 95,
display: "MEN's ITEMS.",
link: "#",
type: "menuitem"
}
]
}
];

假设我想找到 id: 20 的对象并返回:

        {
id: 20,
display: "의류",
link: "#",
type: "menuitem-withmore",
nextItems: [
{
id: 100,
display: "I'm inside one nest",
link: "#",
type: "menuitem"
}
]
},

我似乎找不到如何为此使用 lodash,这个包可能已经解决了我的问题,但我不明白如何让它适用于我的用例。

https://github.com/dominik791/obj-traverse

最佳答案

使用 DFS。

const menuItems = [
{
id: 1,
imageUrl: "http://placehold.it/65x65",
display: "Shop Women",
link: "#",
type: "image",
nextItems: [
{
id: 10,
display: "홈",
link: "#",
type: "menuitem"
},
{
id: 20,
display: "의류",
link: "#",
type: "menuitem-withmore",
nextItems: [
{
id: 100,
display: "I'm inside one nest",
link: "#",
type: "menuitem"
}
]
},
{
id: 30,
display: "가방",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
id: 40,
display: "신발",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
id: 50,
display: "악세서리",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
id: 60,
display: "SALE",
link: "#",
type: "menuitem-withmore",
style: "bold",
nextItems: []
},
{
id: 70,
display: "브랜드",
link: "#",
type: "menuitem-withmore",
nextItems: []
},
{
type: "separator"
},
{
id: 80,
display: "위시리스트",
link: "#",
type: "menuitem"
},
{
id: 90,
display: "고객센터",
link: "#",
type: "menuitem"
},
{
id: 99,
display: "앱 다운로드",
link: "#",
type: "menuitem"
}
]
},
{
id: 2,
imageUrl: "http://placehold.it/65x65",
display: "Shop Men",
link: "#",
type: "image",
nextItems: [
{
id: 95,
display: "MEN's ITEMS.",
link: "#",
type: "menuitem"
}
]
}
];

function dfs(obj, targetId) {
if (obj.id === targetId) {
return obj
}
if (obj.nextItems) {
for (let item of obj.nextItems) {
let check = dfs(item, targetId)
if (check) {
return check
}
}
}
return null
}

let result = null

for (let obj of menuItems) {
result = dfs(obj, 100)
if (result) {
break
}
}

console.dir(result)

关于javascript - 如何在深层嵌套数组中查找具有 id 值的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49308484/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com