有人可以帮助我解释此JavaScript代码中的IF语句吗?

发布于 2025-02-10 04:48:26 字数 427 浏览 1 评论 0原文

function filterArray(array, callback) {
  const newArray = [];
  for (let i = 0; i < array.length; i += 1) {
    if (callback(array[i])) newArray.push(array[i]);
  }
  return newArray;
}

我对语法感到困惑。通常,到目前为止,我所看到的是

if (condition) {
  // code to be executed
}

条件是一些布尔语句,如果条件= true,将运行显示的代码。

但是,在此IF语句中,没有布尔值,在Conditonal语句之后也没有执行代码。我不知道这意味着什么。在此先感谢您对其进行任何帮助。

function filterArray(array, callback) {
  const newArray = [];
  for (let i = 0; i < array.length; i += 1) {
    if (callback(array[i])) newArray.push(array[i]);
  }
  return newArray;
}

I'm confused by the syntax. Typically what I've seen so far is

if (condition) {
  // code to be executed
}

where condition is some boolean statement which will run the code shown if condition = true.

But, in this if statement, there is no boolean, nor is there code to be executed after the conditonal statement. I have no clue what it means. Thanks in advance for any help interpeting it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄离 2025-02-17 04:48:26

(callback(array [i]))此部分是条件。 newarray.push(array [i])这是满足条件时发生的情况。它看起来很近。

if (callback(array[i])) 
{
   newArray.push(array[i])
}

做同样的事情。

(callback(array[i])) this part is the condition. newArray.push(array[i]) this is what happens when the condition is met. It just looks so close together.

if (callback(array[i])) 
{
   newArray.push(array[i])
}

Does same thing.

狼性发作 2025-02-17 04:48:26

一个布尔值,即带有参数array [i]的函数回调返回的值。 (确切地说:根据JavaScript语言规格将返回值解释为真实价值 - 这就是“真实”或“ falsy”的含义。对于开始者,只需假设chardback确实确实,返回一个布尔值)

也正在执行代码:条件之后的第一个语句。
这只是一个符号速记。详细的等效代码将是:

if (callback(array[i])) {
    newArray.push(array[i]);
}

There is a boolean, namely the value returned from calling the function callback with argument array[i]. (To be precise: The interpretation of the returned value as a truth value according to the JavaScript language specs - this is what is meant by 'truthy' or 'falsy'. For starters just assume that callback does indeed return a boolean value)

There also is code being executed: The first statement after the condition.
This is just a notational shorthand. The verbose equivalent code would be:

if (callback(array[i])) {
    newArray.push(array[i]);
}
屋顶上的小猫咪 2025-02-17 04:48:26

if(callback(array [i]))newarray.push(array [i]);仅表示callback是一个函数,被调用,返回值为预期(可能是任何价值,不仅是布尔值)。然后,如果返回为真实,则语句newarray.push(array [i]);被执行。

(单行如果(EXP)语句;可以像这样写,但有时可能会隐藏错误,因此某些Linter倾向于始终将其更改为

if (exp) {
  statement;
}

回调的函数通常是作为参数传递的功能(正如您在这里看到的那样),然后被调用。

if (callback(array[i])) newArray.push(array[i]); simply means that callback is a function, that gets called and a return value is expected (could be any value, not only boolean). Then, if the return is truthy, the statement newArray.push(array[i]); gets executed.

(A single line if (exp) statement; can be written like this, but may sometimes hide bugs, so some linters prefer to always change it to

if (exp) {
  statement;
}

Callbacks are typically functions that get passed in as an argument (exactly as you see here) that then may get called.

呆头 2025-02-17 04:48:26

如果您的病情不起作用,请尝试使用if否则循环。如果您的语句不是您期望显示错误消息,否则按阵列[i];

function filterArray(array, callback) {
  const newArray = [];
  for (let i = 0; i < array.length; i += 1) {
    if (!callback(array[i])){
       console.log('Something went wrong');
  }else{
       newArray.push(array[i]);
    }
  return newArray;
}
´´´

Since you only give this piece of code, it's hard to analyze it, but this may work

If your condition isn't working try to use a if else loop. If your statement isn't what you expect show an error message, else push array[i];

function filterArray(array, callback) {
  const newArray = [];
  for (let i = 0; i < array.length; i += 1) {
    if (!callback(array[i])){
       console.log('Something went wrong');
  }else{
       newArray.push(array[i]);
    }
  return newArray;
}
´´´

Since you only give this piece of code, it's hard to analyze it, but this may work
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文