2017-02-22 42 views
1

外我有一个码通道是这样的:的forEach存储输出到变量和访问匿名功能

tg.router._routes.forEach(function(entry){ 
console.log(entry._commands[0]._textPattern) 
}); 

因为对象(entry._commands [0] ._ textpattern的)内侧三个项目,将的console.log被称为三次。

如何将“entry._commands [0] ._ textPattern”存储到变量(可以是数组)中并在匿名函数之后使用该变量?

我需要一个不同的变体,因为我不想将forEach的输出一次全部传递给处理程序。

回答

4

您可以尝试使用map

如图
const something = tg.router._routes.map(entry => entry._commands[0]._textPattern); 
+0

,这将做完全没有......肯定应该有一个'无功东西='之前代码:对 –

+0

真实的;因为有一个ES6标签,所以增加了'const' :) –

+0

有人会解释我和我的解决方案有什么区别?为什么它假装比我更好的解决方案? –