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

JavaScript dictionary data length

{
    "F1.1": {
        "add": [
            {
                "id": 169138,
                "name": "volkswagen"
            },
            {
                "id": 173212,
                "name": "skoda"
            }
        ],
        "delete": [
            {
                "id": 175555,
                "name": "mercedes"
            }
        ]
    }
}

I’m not been able to get length of add and delete array F1.1 is dynamic value, hence we cannot rely on it.
How can we get the array lengths?

>Solution :

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 data = {
    "F1.1": {
        "add": [
            {
                "id": 169138,
                "name": "volkswagen"
            },
            {
                "id": 173212,
                "name": "skoda"
            }
        ],
        "delete": [
            {
                "id": 175555,
                "name": "mercedes"
            }
        ]
    }
};

// Get the dynamic property names
const dynamicProperties = Object.keys(data["F1.1"]);

// Iterate through the dynamic properties and get the lengths
dynamicProperties.forEach(property => {
    const arrayLength = data["F1.1"][property].length;
    console.log(`Length of ${property} array: ${arrayLength}`);
});
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