JSFiddle - React, Tailwind, and code Playground

by dshilkret

JavaScript

// Sample JSON data for struct1 and struct2
const struct1 = {
    "result": [
        // Example entries from your given struct 1 JSON
        {
            "form_instance": {
                "file_name": {
                    "value": "DD-2875 Access Request.pdf"
                }
            }
        },
        {
            "form_instance": {
                "file_name": {
                    "value": "i-9.pdf"
                }
            }
        }
    ]
};

const struct2 = {
    "data": {
        "d": {
            "results": [
                // Example entries from your given struct 2 JSON
                {
                    "FileLeafRef": "DD-2875 Access Request.pdf"
                },
                {
                    "FileLeafRef": "my-new-dog-pdf-version.pdf"
                }
            ]
        }
    }
};

// Function to extract file names and compare entries from struct1 and struct2
function compareStructs(struct1, struct2) {
    const struct1Files = struct1.result.map(item => item.form_instance.file_name.value);
    const struct2Files = struct2.data.d.results.map(item => item.FileLeafRef);

    const matches = struct1Files.filter(fileName => struct2Files.includes(fileName));

    return matches;
}

// Run the comparison function and log the results
const matchedFiles = compareStructs(struct1, struct2);
console.log("Matched Files:", matchedFiles);