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

Time:08-12

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.

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?

CodePudding user response:

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

  • Related