returning from forEach javascript [duplicate]

i have the following code:

options.forEach((option) => {

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.

Use map instead.

Also, to use map options must be an array.

Check out this example.

var options = Array.from(document.getElementById('selections').options),
    newOptions ={
        return item.value


document.getElementById('results').insertAdjacentHTML('afterbegin', newOptions.reduce((a,b) => a.concat(`${b} `), ' '));
<!DOCTYPE html>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>returning from forEach javascript</title>

    <select id="selections">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
    <div id="results"></div>


Thats what a gold old for loop is good for:

 for(var option of options){
    return option.get("template_name");

Which equals:

 return options[0].get("template_name");

Or getting all names:

 const tnames = option => option.get("template_name") );


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.