【问题标题】:Place items that are not included in Array B at the end of Array A (In a File Tree Structure)将不包含在数组 B 中的项目放在数组 A 的末尾(在文件树结构中)
【发布时间】:2021-11-20 08:27:03
【问题描述】:

我有 2 个不同类型的对象数组。他们都有不同的名称。我想按这些名称比较这两个数组,以移动files 数组底部的fileStructure 数组中未包含的文件(当位于文件夹底部的文件夹中时)。

const files: FileType[] = [
    {
        info: { name: "fileThatsNotIncludedInArray2" },
        contents: [],
    },
    {
        info: { name: "fileThatsIncludedInArray2" },
        contents: [],
    },
    {
        info: { name: "fileThatsIncluded" },
        contents: [
            {
                info: { name: "chapterThatsNotIncludedInArray2" },
                contents: [],
            },
            {
                info: { name: "chapterThatsIncluded" },
                contents: [],
            },

        ],
    },
]

我有以下第二个数组:

const filesStructure: FilesStructure[] = [
    {
        title: "fileThatsIncludedInArray2",
        chapters: []
    },
    {
        title: "fileThatsIncluded",
        chapters:
            [
                {
                    title: "chapterThatsIncluded",
                    chapters: []
                },
            ]
    }
]

预期的结果是这样的:

const finalFiles: FileType[] = [
    {
        info: { name: "fileThatsIncludedInArray2" },
        contents: [],
    },
    {
        info: { name: "fileThatsIncluded" },
        contents: [
            {
                info: { name: "chapterThatsIncluded" },
                contents: [],
            },
            {
                info: { name: "chapterThatsNotIncludedInArray2" },
                contents: [],
            }
        ],
    },
    {
        info: { name: "fileThatsNotIncludedInArray2" },
        contents: [],
    },
]

这是我目前拥有的,但由于某种原因,它只将 1 个项目放在底部,而忽略了其余项目:

export function placeUnIncludedElementsAtTheEndOfTheSortedArray(
    sortedArray: FileType[],
    fileStructure: FilesStructure[],
): FileType[] {
    for (const sortedFile of sortedArray) {
        const findInd = fileStructure.findIndex((fr) => {
            return fr.title == sortedFile.info.name
        })

        if (findInd == -1) {
            sortedArray.push(sortedArray.splice(sortedArray.indexOf(sortedFile), 1)[0])
        }
    }

    return sortedArray
}

【问题讨论】:

  • 谢谢,我已经更新了问题

标签: javascript arrays typescript sorting


【解决方案1】:

您需要按另一个数组排序并将缺少的值移到最后:

const sorted = ft.sort((a, b) => {
    const _a = fs.findIndex(s => s.title === a.info.name);
    const _b = fs.findIndex(s => s.title === b.info.name);
    return (_a == -1 || _b == -1) ? (_a == -1 ? 1 : -1) : (_a - _b);
});

完整的例子是:

function changeStructure(ft: FileType[], fs: FilesStructure[]): FileType[] {
    // Sort by other array, move last missing values
    const sorted = ft.sort((a, b) => {
        const _a = fs.findIndex(s => s.title === a.info.name);
        const _b = fs.findIndex(s => s.title === b.info.name);
        return (_a == -1 || _b == -1) ? (_a == -1 ? 1 : -1) : (_a - _b);
    });
    // Recursive
    sorted.forEach(e => {
        const s = fs.find(s => s.title === e.info.name);
        if (s) e.contents = changeStructure(e.contents, s.chapters);
    })

    return sorted;
}

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2020-01-06
    • 1970-01-01
    相关资源
    最近更新 更多