Deltahost
Deltahost

Reputation: 117

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.

Upvotes: 1

Views: 1137

Answers (1)

Christian Zosel
Christian Zosel

Reputation: 1424

You can try using map:

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

Upvotes: 4

Related Questions