简体   繁体   English

在找到的示例代码中,Array.prototype.find 的箭头 function 回调中令人惊讶的语法是什么?

[英]Within a found example code, what is the surprising syntax in an arrow function callback of Array.prototype.find for?

I found the following example code, and I want to figure out what it does.我找到了以下示例代码,我想弄清楚它的作用。

projects.find( p => p.value === 'jquery-ui' && ( p.desc = 'your value', true ) );

What is the last true from ( p.desc = 'your value', true ) for? ( p.desc = 'your value', true )的最后一个true是什么? I know that find should return just the first matching array item but I see, with the use of the provided example code, it did not only find the object but it did also change it.我知道find应该只返回第一个匹配的数组项,但我看到,使用提供的示例代码,它不仅找到了 object,而且还改变了它。

This is actually quite terrible code.这实际上是非常糟糕的代码。 ಠ_ಠ ಠ_ಠ

First, the comma operator evaluates all its operands and returns the last one.首先,逗号运算符评估其所有操作数并返回最后一个。 Ie, p.desc = 'your value', true assigns 'your value' to p.desc and returns true .p.desc = 'your value', true'your value'分配给p.desc并返回true This is chained to the p.value === 'jquery-ui' check, so only executes when that expression is true .这链接到p.value === 'jquery-ui'检查,因此仅在该表达式为true时执行。 The entire thing is in a find , which iterates over all items until it finds an item where the callback function returns true .整个事情都在find中,它遍历所有项目,直到找到回调 function 返回true的项目。

In other words, it's using Array.prototype.find to iterate over all the array entries, and when it finds an entry whose .value is 'jquery-ui' , it updates that entry by setting .desc , then ends the find iteration by returning true .换句话说,它使用Array.prototype.find遍历所有数组条目,当它find一个.value'jquery-ui'的条目时,它通过设置.desc更新该条目,然后通过返回true

Which is actually quite clever, but:这实际上很聪明,但是:

  1. Introduces a side effect into what should be a pure test callback function and副作用引入应该是纯测试回调 function 和
  2. leads to questions like this because it's unnecessarily clever and hard to comprehend at first sight.导致这样的问题,因为它不必要地聪明且乍一看难以理解。

This is a way to find the first object with p.value === 'jquery-ui' and update its desc property to 'your value' .这是一种使用 p.value p.value === 'jquery-ui'找到第一个 object 并将其desc属性更新为'your value'方法。

It's equivalent to:它相当于:

const found = projects.find(p => p.value === 'jquery-ui');

if (found)
  found.desc = 'your value'
  • First, it checks the p.value === 'jquery-ui' condition.首先,它检查p.value === 'jquery-ui'条件。 If it returns false, && will short-circuit and will not check the second block.如果它返回 false, &&将短路并且不会检查第二个块。 It goes over to the next object in the array.它转到阵列中的下一个 object。

  • If p.value === 'jquery-ui' is true for some object in the array, && will execute the second expression ( p.desc = 'your value', true ) .如果p.value === 'jquery-ui'对于数组中的某些 object 为 true,则&&将执行第二个表达式( p.desc = 'your value', true ) This uses the comma operator .这使用逗号运算符 It evaluates each expression within () and returns the final value.它计算 () 中的每个表达式并返回最终值。 It assigns a new value to the found object and returns true .它为找到的 object 分配一个新值并返回true This is done to short-circuit the find call so that it stops running this callback on any more objects in the array.这样做是为了使find调用短路,以便它停止在数组中的任何更多对象上运行此回调。

  • The comma operator is ONLY required if 'your value' is falsy .仅当'your value'falsy 时才需要逗号运算符。 In your case, it's a non-empty string.在您的情况下,它是一个非空字符串。 It is redundant and no different to using它是多余的,与使用没有什么不同

     p.value === 'jquery-ui' && p.desc = 'your value'

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

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