0

i have an object im looping through with a map

datas.children.map((datasAgain) => {
  if (datasAgain returns nothing) {
    // do something
   }
   return // do another thing
 })

datas.children is inside another map fuction and returns multiple lists of objects, one of those lists is empty "[]", datasAgain returns objects inside of datas.children, how can i check if datasAgain returns nothing here? essentially checking if the list in datas.children is empty

me-an-ape
  • 57
  • 1
  • 5
  • `if (datasAgain.length == 0)` ? – Mark Reed Jun 22 '23 at 03:04
  • Does this answer your question? [How to check if array is empty or does not exist?](https://stackoverflow.com/questions/24403732/how-to-check-if-array-is-empty-or-does-not-exist) – Mark Reed Jun 22 '23 at 03:05
  • if the list inside datas.children is empty, somehow it returnsn nothing, not even undefined or null, so i can't even check the length here, i can't check the length if it doesn't exist – me-an-ape Jun 22 '23 at 03:06
  • i've found a solution here which is to check the datas.children.length first then i would do the map, but if datas.children.length === 0 i would do nothing, but im still kind of curios here, is there nothing i can do if i want to check if datasAgain literally does nothing? (not even returning undefined/null, etc) – me-an-ape Jun 22 '23 at 03:12
  • If `datas.children` is an empty list, then `map` will never ever call the function you pass to it, because there's no argument to send to that function. So it doesn't matter what code you put in there, it will never be executed. I thought you meant `datas.children` was a list _containing_ an empty list inside of it, in which case it would call the map function with that empty list as `datasAgain` and my comment above would be useful. – Mark Reed Jun 22 '23 at 14:58

3 Answers3

1

Before you do the loop, compare using the code below:

if(datasAgain.length == 0){

// your map

}

Tim Muia
  • 183
  • 1
  • 7
  • yup i just realised that after i asked the question, but im still curios though, is there a way to check if something doesnt return anything/does nothing? – me-an-ape Jun 22 '23 at 03:14
0

To check if datasAgain returns nothing gives an array, you should check it is array and its length is not 0. Here is an example of how to do:

datas.children.map(datasAgain => {
  if (!Array.isArray(datasAgain)) {
    // do something when datasAgain is not array
  } else if (Array.isArray(datasAgain) && datasAgain.length === 0) {
    // do something when datasAgain's length is equal to 0
  }
  return // do another thing
})

In this code block, we first ensure that datasAgain is actually an array by calling the Array.isArray() method. If it is an array, then we check its length property to see if it equals zero. If it does equal zero, then we know the list is empty and can perform any necessary actions in the if block. If it has items, it will fall through to the else block where you can do whatever else is needed.

I hope this will helpful for you. Thank you.

hashi7412
  • 1
  • 1
-2
datas.children.map((datasAgain) => {
  if (datasAgain.length === 0) {
    // The list in datas.children is empty
    // Do something
  } else {
    // The list in datas.children is not empty
    // Do another thing
  }
  return // return value (if needed)
});
Anass
  • 2,101
  • 2
  • 15
  • 20