简体   繁体   中英

forEach store output into variable and access outside anonymous function

I have a code passage like this:

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

Because inside the object (entry._commands[0]._textPattern) are three items, console.log will be called three times.

How can I store "entry._commands[0]._textPattern" into a variable (could be an array) and use the variable later outside of the anonym function?

I need a different variant, because I wan't to pass the output of the forEach later all at once to a handler.

您可以尝试使用map

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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