我已经了解了如何使用filter
,但是看到此练习的答案是:Eloquent JavaScript使我感到困惑(单击“查看解决方案”时,您可以看到代码。之间的区别:
let counted = countBy(text, char => {
let script = characterScript(char.codePointAt(0));
return script ? script.direction : "none";
}).filter(({name}) => name != "none");
和:
let counted = countBy(text, char => {
let script = characterScript(char.codePointAt(0));
return script ? script.direction : "none";
}).filter(name => name != "none"); //The difference is here and when I tried this, it did not work
我所熟悉的是使用... .filter(something => condition);
,它将返回满足上述条件的原始数组中所有项目的数组。我尝试将上面的代码更改为... .filter(script => script.name != "none")
并成功。那么({name})
到底是怎么做的而不必键入那么长的内容?
答案 0 :(得分:1)
实际上是对象的破坏。无需将对象x
传递到过滤器中然后与x.name
进行比较,而是直接对name
进行了破坏。
.filter(({name}) => name != "none");
等效于
.filter(x => x.name != "none");
有关解构here
的更多信息