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

How to filter array to know if multiChild array consists ONLY of children of a certain type?

I have a data structure like:

  const Items = [
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'cat' }, { type: 'cat' }, { type: 'cat' }] }
      ]
    },
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    },
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    }
  ];

I have 3 objects that are of type cat

Im tryin to figure out how can I write a filtering algoritm that tells me if my array consist only of child objects that are if type cat.

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

So far:

  function isAllOfType(type: string) {
    for (let i = 0; i <= Items.length; i++) {
      for (let i2 = 0; i2 <= Items[i].coolitmes.length; i2++) {
        for (let i3 = 0; i3 <= Items[i].coolitmes[i2].coolerItems.length; i3++) {
          if (Items[i].coolitmes[i2].coolerItems[i3].type === type) {
            // what do I do here
          }
        }
      }
    }

    // return true/false depending if all chidlren are of certian type
  }

const isAllCat = isAllOfType('cat')

Im not sure im thinking about it right. I need to know the total number of dog types and then the total number of cat types and if there are no dog types and only cat types then I know. How do I accomplish this?

>Solution :

Really, this just boils down to 1 line:

Items.flatMap(x => x.coolitmes).flatMap(x => x.coolerItems).every(x => x.type == type);

And this can be wrapped in a function as below:

// Mostly dogs, but some cats
const Items = [
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'cat' }, { type: 'cat' }, { type: 'cat' }] }
      ]
    },
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    },
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    }
  ];
  
  // All dogs
  const Items2 = [
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    },
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    },
    {
      coolitmes: [
        {
          coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
        },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
        { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
      ]
    }
  ];
  
  function isAllOfType(items, type){
      return items.flatMap(x => x.coolitmes).flatMap(x => x.coolerItems).every(x => x.type == type);
  }
  
  console.log(isAllOfType(Items,"cat"));
  console.log(isAllOfType(Items2,"dog"));
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