2

I have an array and inside i have a group of objects and how would i loop through them and to make a check for which one of the objects have a status as true?

menuItems: [
  {
    text1: 'Apple',
    status: false,
    subItems: []
  },
  {
    text2: 'Orange',
    status: false,
    subItems: []
  },
  {
   text3: 'Banana',
    status: true,
    subItems: []
  }
]

I tried something like this:-

if(menuItems.forEach(active) === 'false') {
then('do something!!!')
}
  • 2
    i would use filter if i was you. `let trueItems = menuItems.filter(item => item.status);` If you wanted to take it one step further, follow with map. `trueItems.map( item => processForTrueItem(item) );` – Jacob Thomas May 14 '19 at 20:06
  • Is this some assignment that requires you to use `forEach`? It's possible to do, so but `forEach` may not be the best tool for the job. – Scott Sauyet May 14 '19 at 20:07
  • Is there guaranteed to be just one item with `status` of `true`, or could there be several? – Scott Sauyet May 14 '19 at 20:09
  • @Scott yeah there could be several, this is just a short example. –  May 14 '19 at 20:10
  • Would you also have to find the `true` statuses among any `subItems`? That would best be handled recursively. – Scott Sauyet May 14 '19 at 20:12
  • no need to worry about subItems. I might not even need it. Just threw that in there. but if you want to do a separate answer. sure by all means, –  May 14 '19 at 20:13
  • Just trying to tease out your requirements. Is `forEach` necessary for any reason? Or could you use `filter`? – Scott Sauyet May 14 '19 at 20:15
  • which ever one you think is the easiest will do. –  May 14 '19 at 20:18
  • @ScottSauyet actually if you could show the filter way, that might work too. –  May 14 '19 at 20:24

2 Answers2

1

An example:

const menuItems = [
  {
    text: 'Apple',
    status: false,
    subItems: []
  },
  {
    text: 'Orange',
    status: false,
    subItems: []
  },
  {
   text: 'Banana',
    status: true,
    subItems: []
  }
];

menuItems.forEach(menuItem => { if(menuItem.status) { console.log(menuItem.text) } });

Read MDN!

https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

connexo
  • 53,704
  • 14
  • 91
  • 128
1

You have a couple options. On the top level, you could add an if statement to the function passed to forEach:

menuItems.forEach((item) => {
  if (item.status) {
    console.log('Do something') // only logs when item.status is true
  }
})

Note that your sample code: menuItems.forEach(active) === 'false') would not work even after fixing the syntax, because in your sample data status is a boolean, and you're trying to compare it to a string with ===.

Back to the main question though: Instead of having an if statement, you could use the filter method to first remove any items that don't have a status of true:

menuItems.filter(item => item.status).forEach((item) => {
  // The filter function will remove any items where item.status is falsy,
  // leaving only items where status is true.
  console.log('Do something')
})
Nathan Gingrich
  • 171
  • 1
  • 6