i have the following code:
options.forEach((option) => {
return(option.get('template_name'))
})
where options contains a list of 2 maps
I expect this to return the template name, but instead I get 2
Why is this? How can I return from a forEach
function in javascript?
forEach
does not return. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/forEach#Return_valueUse
map
instead. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/map#Return_valueAlso, to use
map
options must be an array.Check out this example.
Thats what a gold old for loop is good for:
Which equals:
Or getting all names: