在对象数组中查找路径

时间:2019-08-27 17:01:57

标签: javascript

我需要在对象数组中找到一条路径,但是我不知道该怎么做。

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>

successPathpath相同,因此,如果在当前示例中仅找到一个路径:

successPath[0] = ["1", "5", "7", "2"]

感谢您的帮助或提示!

0 个答案:

没有答案