Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

exclude empty objects from result set when using .reduce()

The .reduce() method is being employed to perform data validation. Below is a simplified sample of the data in the preMigration and postMigration objects. The actual dataset has several more pairs in the "line" and "profile" parts.

Although the math is being performed correctly, the resulting output of the deepSubtract() method includes empty objects ({}) for "line" and for the indexes within "profile" when the results from both objects match. For the live version of the data, which is lengthy, this is problematic as the volume of data to review is huge and most of it is empty objects.

Is there a way to modify the logic of deepSubract() to exclude empty objects from the final output? The desired result for the given example is displayed below the code snippet.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const preMigration = {
    "100": {
        "line": {
            "machines": 1,
            "operators": 4
        },
        "profile": [
            {
                "capacity": 0.8,
                "downtimeAllowance": 0.99
            },
            {
                "capacity": 0.95,
                "downtimeAllowance": 0.7
            }
        ]
    },
    "200": {
        "line": {
            "machines": 6,
            "operators": 12
        },
        "profile": [
            {
                "capacity": 0.9,
                "downtimeAllowance": 0.85
            }
        ]
    },
    "300": {
        "line": {
            "machines": 9,
            "operators": 3
        },
        "profile": [
            {
                "capacity": 0.85,
                "downtimeAllowance": 0.71
            },
            {
                "capacity": 0.94,
                "downtimeAllowance": 0.97
            }
        ]
    }
}

const postMigration = {
    "100": {
        "line": {
            "machines": 1,
            "operators": 0
        },
        "profile": [
            {
                "capacity": 0.8,
                "downtimeAllowance": 0.99
            },
            {
                "capacity": 0.95,
                "downtimeAllowance": 0.7
            }
        ]
    },
    "200": {
        "line": {
            "machines": 6,
            "operators": 12
        },
        "profile": [
            {
                "capacity": 0.9,
                "downtimeAllowance": 0.85
            }
        ]
    },
    "300": {
        "line": {
            "machines": 9,
            "operators": 5
        },
        "profile": [
            {
                "capacity": 0.85,
                "downtimeAllowance": 0.71
            },
            {
                "capacity": 0.96,
                "downtimeAllowance": 0.97
            }
        ]
    }
}

this.deepSubtract = (obj1, obj2) => {
    const uniqueKeys = [...new Set([...Object.keys(obj1), ...Object.keys(obj2)])];
    const reduction = uniqueKeys.reduce((acc, key) => {
        if (typeof obj2[key] === 'object') {
            acc[key] = this.deepSubtract(obj1[key], obj2[key]);
        } else {
            if (obj1[key] !== obj2[key]) acc[key] = obj1[key] - obj2[key]
        }
        return acc;
    }, {});
    return reduction;
}

const dataValidation = deepSubtract(postMigration, preMigration);
console.log(JSON.stringify(dataValidation, null, 4));

Desired output for this example:

{
    "100": {
        "line": {
            "operators": -4
        }
    },
    "300": {
        "line": {
            "operators": 2
        },
        "profile": {
            "1": {
                "capacity": 0.020000000000000018
            }
        }
    }
}

>Solution :

To modify the logic of deepSubtract() to exclude empty objects from the final output, you can add a condition to check if the object is empty before returning it. Here is an example of how you could do this:

this.deepSubtract = (obj1, obj2) => {
    const uniqueKeys = [...new Set([...Object.keys(obj1), ...Object.keys(obj2)])];
    const reduction = uniqueKeys.reduce((acc, key) => {
        if (typeof obj2[key] === 'object') {
            const subtractedObject = this.deepSubtract(obj1[key], obj2[key]);
            // Check if the subtracted object is empty
            if (Object.keys(subtractedObject).length > 0) {
                acc[key] = subtractedObject;
            }
        } else {
            if (obj1[key] !== obj2[key]) acc[key] = obj1[key] - obj2[key]
        }
        return acc;
    }, {});
    return reduction;
}

const dataValidation = deepSubtract(postMigration, preMigration);
console.log(JSON.stringify(dataValidation, null, 4));

In this updated version of deepSubtract(), after subtracting the nested objects, we check if the resulting object is empty by checking if it has any keys. If it is empty, we do not include it in the final result. This will ensure that empty objects are not included in the output.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading