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 – How to loop through objects within objects that also contain arrays?

Let’s say I have an object that contains objects as well as arrays —

const models = {
    characters: {
        human: {
            male: {
                short: [
                    { url: 'resources/models/characters/human/male/short/male0.fbx' },
                ],
                tall: [
                    { url: 'resources/models/characters/human/male/tall/male0.fbx' },
                ]
            },
            female: [
                { url: 'resources/models/characters/human/female/female0.fbx' },
                { url: 'resources/models/characters/human/female/female0.fbx' },
            ],
        },
        orc: {
            male: [
                
            ],
            female: [
                
            ],
        }
    },
    objects: {
        floors: {
            wood: [
                { url: 'resources/models/objects/floors/wood/floor_wood_000.obj' },
                { url: 'resources/models/objects/floors/wood/floor_wood_001.obj' },
                { url: 'resources/models/objects/floors/wood/floor_wood_002.obj' },
                { url: 'resources/models/objects/floors/wood/floor_wood_003.obj' }
            ],
        },
        walls: {
            wood: [
                
            ],
        },
        stairs: {
            wood: [
                
            ],
        },
        bed: [
            { url: 'resources/models/objects/bed/objects_bed_mattress_000.obj' },
            { url: 'resources/models/objects/bed/objects_bed_mattress_000.obj' }
        ],
    }
};

var count = {
    characters: numberOfUrlsInCharacters,
    objects: numberofUrlsInObjects
};

Now I want to count how many URLs in total the object models contains. Would I have to count this manually, or is there an easier way?

Also, notice the objects are not equal. They all contain different amount of objects & arrays.

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

>Solution :

Since you mentioned that the structure of the data is not consistent, it would make your task very easy if you stringify the object, and then look for the keyword "url": to get a count of the URLs present.

const strigifiedModels = JSON.stringify(models);

const count = (strigifiedModels.match(/"url":/g) || []).length;

Working snippet:

const models = {
  characters: {
    human: {
      male: {
        short: [{
          url: 'resources/models/characters/human/male/short/male0.fbx'
        }, ],
        tall: [{
          url: 'resources/models/characters/human/male/tall/male0.fbx'
        }, ],
        female: [{
            url: 'resources/models/characters/human/female/female0.fbx'
          },
          {
            url: 'resources/models/characters/human/female/female0.fbx'
          },
        ],
      },
      orc: {
        male: [],
        female: [],
      },
    },
    objects: {
      floors: {
        wood: [{
            url: 'resources/models/objects/floors/wood/floor_wood_000.obj'
          },
          {
            url: 'resources/models/objects/floors/wood/floor_wood_001.obj'
          },
          {
            url: 'resources/models/objects/floors/wood/floor_wood_002.obj'
          },
          {
            url: 'resources/models/objects/floors/wood/floor_wood_003.obj'
          },
        ],
      },
      walls: {
        wood: [],
      },
      stairs: {
        wood: [],
      },
      bed: [{
          url: 'resources/models/objects/bed/objects_bed_mattress_000.obj'
        },
        {
          url: 'resources/models/objects/bed/objects_bed_mattress_000.obj'
        },
      ],
    },
  },
};

const strigifiedModels = JSON.stringify(models);

const count = (strigifiedModels.match(/"url":/g) || []).length;

console.log(count);
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