Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

302
Views
Place items that are not included in Array B at the end of Array A (In a File Tree Structure)

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
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!