Home > Software engineering >  Javascript - How to loop through objects within objects that also contain arrays?
Javascript - How to loop through objects within objects that also contain arrays?

Time:05-11

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.

CodePudding user response:

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);

CodePudding user response:

Recursive Nonsense:

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 countUrls = (thing) => {
  let count = 0;
  if (Array.isArray(thing)) {
    count  = thing.reduce((acc, el) => acc   countUrls(el), 0);
  } else if (typeof thing === "object") {
    if (thing.url) {
      count  ;
    } else {
      count  = Object.values(thing).reduce((acc, el) => acc   countUrls(el), 0);
    }
  }
  return count;
};

console.log(countUrls(models));

console.log(countUrls(models.characters.human));

CodePudding user response:

Try this :

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 numberOfUrlsInCharacters = JSON.stringify(models.characters).match(/"url":/ig).length;
const numberofUrlsInObjects = JSON.stringify(models.objects).match(/"url":/ig).length;

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

console.log(count);

CodePudding user response:

Recursive look up. Just assumes that url will aways be in array of objects.

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 countUrls = (item) => {
  if (Array.isArray(item)) {
    return item.reduce((c, x) => x.url ? c   1 : c, 0);
  } else {
    return Object.values(item).reduce((cnt, x) =>
      cnt   countUrls(x), 0);
  }
}


const result = Object.entries(models).reduce((acc, [key, obj]) => {
  acc[key] = countUrls(obj);
  return acc;
}, {});

console.log(result);

  • Related