简体   繁体   English

有人可以帮我解释这个 javascript 代码中的 if 语句吗?

[英]Can someone help me interpret the if statement in this javascript code?

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.其中条件是一些布尔语句,如果条件 = true,它将运行显示的代码。

But, in this if statement, there is no boolean, nor is there code to be executed after the conditonal statement.但是,在这个 if 语句中,没有布尔值,也没有在条件语句之后执行的代码。 I have no clue what it means.我不知道这意味着什么。 Thanks in advance for any help interpeting it.在此先感谢您提供的任何帮助。

(callback(array[i])) this part is the condition. (callback(array[i]))这部分是条件。 newArray.push(array[i]) this is what happens when the condition is met. newArray.push(array[i])这是满足条件时发生的情况。 It just looks so close together.它看起来如此接近。

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

Does same thing.做同样的事情。

There is a boolean, namely the value returned from calling the function callback with argument array[i] .一个布尔值,即使用参数array[i]调用函数callback返回的值。 (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) (准确地说:根据 JavaScript 语言规范将返回值解释为真值 - 这就是“真实”或“虚假”的含义。对于初学者来说,只需假设callback确实返回一个布尔值)

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]);
}

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).仅仅意味着callback是一个函数,它被调用并且期望返回值(可以是任何值,而不仅仅是布尔值)。 Then, if the return is truthy , the statement newArray.push(array[i]);然后,如果返回为,语句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;可以这样写,但有时可能会隐藏错误,因此一些 linter 更喜欢总是将其更改为

if (exp) {
  statement;
}

Callbacks are typically functions that get passed in as an argument (exactly as you see here) that then may get called.回调通常是作为参数传入的函数(正如您在此处看到的那样),然后可能会被调用。

If your condition isn't working try to use a if else loop.如果您的条件不起作用,请尝试使用 if else 循环。 If your statement isn't what you expect show an error message, else push array[i];如果您的陈述不是您所期望的,则显示错误消息,否则 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM