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

Array iteration inside array of objects

I need to write a function that will go through all array objects and check wether at least one object has inner array where all objects have boolean value set to true. Please see code examples for better understanding.

Example 1

const array = [
  {
    id: 1,
    innerArray: [
      { innerId: 1, clicked: false },
      { innerId: 2, clicked: false },
    ],
  },
  {
    id: 2,
    innerArray: [
      { innerId: 1, clicked: true },
      { innerId: 2, clicked: false },
    ],
  },
  {
    id: 3,
    innerArray: [
      { innerId: 1, clicked: true },
      { innerId: 2, clicked: true }, 
    ],
  },
];

functionToBeCreated(array); // Output: true - because item with id 3 has innerArray where all items have "clicked: true".

Example 2

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 array = [
  {
    id: 1,
    innerArray: [
      { innerId: 1, clicked: false },
      { innerId: 2, clicked: false },
    ],
  },
  {
    id: 2,
    innerArray: [
      { innerId: 1, clicked: true },
      { innerId: 2, clicked: false },
    ],
  },
  {
    id: 3,
    innerArray: [
      { innerId: 1, clicked: false },
      { innerId: 2, clicked: true }, 
    ],
  },
];

functionToBeCreated(array); // Output: false - because no item has innerArray where all items have "clicked: true".

Do you have maybe any ideas how it ca be achieved?

>Solution :

this way…

const ToBeCreated = arr =>
   arr.some( el => el.innerArray.every(z => z.clicked));

const arrayTRUE = 
  [ { id: 1, innerArray: 
      [ { innerId: 1, clicked: false } 
      , { innerId: 2, clicked: false }
      ] } 
  , { id: 2, innerArray: 
      [ { innerId: 1, clicked: true  } 
      , { innerId: 2, clicked: false } 
    ] } 
  , { id: 3, innerArray: 
      [ { innerId: 1, clicked: true } 
      , { innerId: 2, clicked: true } 
  ] } ] 

const arrayFALSE = 
  [ { id: 1, innerArray: 
      [ { innerId: 1, clicked: false } 
      , { innerId: 2, clicked: false }
      ] } 
  , { id: 2, innerArray: 
      [ { innerId: 1, clicked: true  } 
      , { innerId: 2, clicked: false } 
    ] } 
  , { id: 3, innerArray: 
      [ { innerId: 1, clicked: false } 
      , { innerId: 2, clicked: true  } 
  ] } ] 

// test 
console.log( ToBeCreated(arrayTRUE ) ) 
console.log( ToBeCreated(arrayFALSE) ) 
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