我需要在对象数组中找到一条路径,但是我不知道该怎么做。
let path = ["1", "5", "7", "2"];
let successPath = [];
const possibleWays = [
{
number: "2",
canBeNext: [
{
number: "3",
canBeNext: [
number: "5",
canBeNext: [
{
number: "9"
}
]
},
{
number: "6",
canBeNext: [
{
number: "2",
canBeNext: [
{
number: "7"
}
]
},
{
number: "6",
canBeNext: [
{
number: "6"
}
]
}
]
}
]
},
{
number: "1",
canBeNext: [
{
number: "5",
canBeNext: [
number: "7",
canBeNext: [
{
number: "9"
}
]
},
{
number: "5",
canBeNext: [
{
number: "2",
canBeNext: [
{
number: "7"
}
]
},
{
number: "6",
canBeNext: [
{
number: "5"
}
]
}
]
}
]
},
{
number: "1",
canBeNext: [
{
number: "1",
canBeNext: [
number: "5",
canBeNext: [
{
number: "1"
}
]
},
{
number: "5",
canBeNext: [
{
number: "2",
canBeNext: [
{
number: "7"
}
]
},
{
number: "7",
canBeNext: [
{
number: "2"
}
]
}
]
}
]
}
]
如果successPath
中不存在path
,则 possibleWays
将为空。
仅找到一条路径时:successPath[0] = <theRightPath>
找到一条路径时,successPath[i] = <theRightPath>
successPath
与path
相同,因此,如果在当前示例中仅找到一个路径:
successPath[0] = ["1", "5", "7", "2"]
感谢您的帮助或提示!