JavaScript – 如何检查我在使用map循环时的值是否存在或不存在

huangapple go评论71阅读模式
英文:

javascript - how to check if the value im looping with a map exists or not

问题

我有一个对象,我正在使用map循环遍历它

datas.children.map((datasAgain) => {
  if (datasAgain 返回空值) {
    // 做某事
  }
  return // 做另一件事
})

datas.children位于另一个map函数内,返回多个对象列表,其中一个列表是空的"[]"
datasAgain返回datas.children中的对象,如何检查这里datasAgain是否返回空值?本质上是检查datas.children中的列表是否为空。

英文:

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

答案1

得分: 1

在执行循环之前,请使用以下代码进行比较:

if (datasAgain.length == 0){

    // 你的映射
}
英文:

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

if(datasAgain.length == 0){

// your map

}

答案2

得分: 0

要检查datasAgain是否返回空值并给出一个数组,您应该检查它是否是数组,且其长度不为0。以下是如何进行的示例:

datas.children.map(datasAgain => {
  if (!Array.isArray(datasAgain)) {
    // 当datasAgain不是数组时执行某些操作
  } else if (Array.isArray(datasAgain) && datasAgain.length === 0) {
    // 当datasAgain的长度等于0时执行某些操作
  }
  return // 执行其他操作
})

在这段代码中,我们首先通过调用Array.isArray()方法确保datasAgain实际上是一个数组。如果它是一个数组,然后我们检查其长度属性,以查看它是否等于零。如果等于零,那么我们知道列表是空的,并可以在if块中执行任何必要的操作。如果它有项目,它将继续执行else块,您可以在其中执行其他所需操作。

希望这对您有帮助。谢谢。

英文:

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:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

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

<!-- end snippet -->

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.

答案3

得分: -2

datas.children.map((datasAgain) => {
  if (datasAgain.length === 0) {
    // datas.children中的列表为空
    // 做一些操作
  } else {
    // datas.children中的列表不为空
    // 做另一些操作
  }
  return // 返回值(如果需要)
});
英文:
datas.children.map((datasAgain) =&gt; {
  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)
});

huangapple
  • 本文由 发表于 2023年6月22日 11:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定