Home > Back-end >  Place items that are not included in Array B at the end of Array A (In a File Tree Structure)
Place items that are not included in Array B at the end of Array A (In a File Tree Structure)

Time:09-28

I have 2 arrays of different types of objects. They both have a title under different names. I'd like to compare these two arrays by these names to move files that are not included in the fileStructure array at the bottom of the files array (when in a folder at the bottom of the folder).

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

        ],
    },
]

And I have the following second array:

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

The expected result would be something like this:

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

This is what I currently have but for some reason, it only places 1 of the items at the bottom and leaves out the rest:

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
}

CodePudding user response:

You need to sort by another array and move as last the values that are missing:

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);
});

The complete example is:

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;
}
  • Related