.forEach iteration in ES6 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

.forEach iteration in ES6

How do i iterate an array .forEach function and use it to populate a select element with options

7th May 2019, 11:42 PM
Samuel Nnaji
20 Answers
+ 8
Any sample input and their respective outputs? Your question is not very clear.
8th May 2019, 12:18 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 8
Lord Krishna Well, the forEach() method doesn't actually return anything (undefined). It simply calls a provided function on each element in your array. This callback is allowed to mutate the calling array. ... The difference is thatmap() utilizes return values and actually returns a new Array of the same size (Google)
8th May 2019, 2:28 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 5
@Calviղ Why is "map" the better way instead of "forEach"? The logic appears to be quite similar and output just the same. html += cars.map( c=> `<option value="${c}">${c}</option>` ).join(''); cars.forEach( c=> html+=`<option value="${c}">${c}</option>` );
8th May 2019, 1:59 AM
Lord Krishna
Lord Krishna - avatar
+ 4
Thanks for the replies! It appears map is better suited for simply mapping functions to arrays. Where forEach requires writing code to manage the output. //demo code let a = [2, 4, 9, 8, 1] console.log(a.map(x=>x*x)) console.log(a)//a still the same a.forEach((x, i)=>a[i] = x*x) console.log(a)//original array modifed
8th May 2019, 3:24 AM
Lord Krishna
Lord Krishna - avatar
+ 3
Here you go https://code.sololearn.com/W8c03B3D21M2/?ref=app However map function should be the better way, instead of forEach function. https://code.sololearn.com/WeTPrPW9rlxh/?ref=app
8th May 2019, 1:00 AM
Calviղ
Calviղ - avatar
+ 3
Lord Krishna map is pure function, whereas forEach is not pure function. The definition of pure function is 1. its return value is only determined by its input values. 2. its return value is always the same for the same input values. We should always choose to use pure function, you can't see any advantages from a simple program here. Pure function would become important, when come to building components and modules. All React pure components are built by pure functions. You would see a lot of map functions around react.js codes, but not seeing any forEach function there. Study below expression, var output = input.map(transformationMethod); We can get the output result without understand or change any logic in transformationMethod. However var output = input.forEach(transformationMethod); output is undefined. We would then need to dissect the logics behind transformationMethod in order to add output and get result.
8th May 2019, 2:49 AM
Calviղ
Calviղ - avatar
+ 3
Lord Krishna you're right.. Sometime ForEach is applicable to form prototype for a class function, where its properties can be altered from the forEach logics. But it should not be implemented as direct access function.
8th May 2019, 3:33 AM
Calviղ
Calviղ - avatar
+ 3
So this thread is solved with Calviղ's first linked code, correct?
8th May 2019, 1:21 PM
Janning⭐
Janning⭐ - avatar
+ 3
Is this homework?
8th May 2019, 10:17 PM
Janning⭐
Janning⭐ - avatar
+ 3
Oh, then I wouldn't want us to take away from your experience and getting test results that accurately reflect your abilities. https://www.sololearn.com/Blog/38/8-simple-rules-to-get-help-from-the-community One of the headings says "Don't post your homework". 🤷🏻‍♂️
8th May 2019, 10:24 PM
Janning⭐
Janning⭐ - avatar
+ 2
Ok i have an empty const arr = [], then i want to use .forEach function to iterate over the array while adding <option> to my select element in my UI
8th May 2019, 12:40 AM
Samuel Nnaji
+ 1
Thanks for the replies but the requirement is to use a forEach function and pls using ES6 syntax no jquery
8th May 2019, 10:26 AM
Samuel Nnaji
+ 1
Yes kind of a test
8th May 2019, 10:18 PM
Samuel Nnaji
+ 1
JavaScript ES 6 (ECMAScript6) Loops and Function in ES6 const printOdds = (arr) => { arr.forEach(el=>{ if (el % 2 != 0) console.log(el); }); }
17th May 2020, 2:29 PM
Adam Ismail
Adam Ismail - avatar
+ 1
const printOdds = (arr) => { arr .forEach( el => { if (el % 2 != 0) console.log(el); }); }
28th Jun 2020, 8:32 AM
Faith Mundi
Faith Mundi - avatar
0
Step 3 The fetchAndDisplayUsers tries to call a displayUsers function that does not exist. Lets fix that! Create a displayUsers arrow function just above the fetchAndDisplayUsers function. It should take a users parameter It should use the array .forEach function to iterate over users and populate the SELECT UI element with OPTION elements. Each OPTION should have its value set to the id of a given user, while its display text should be set to the user's name We can see a sample user in our app; a certain Charles Odili from Nigeria! Add a new user object (e.g representing yourself) to the users array in fetchAndDisplayUsers. Feel free to not use real data values, but ensure the id is different from that of the existing user. Step 4 When we select the sample user from the list, nothing happens. Let's fix that! Create a getSelectedUser arrow function above displaySelectedUser. It should take a userId parameter and use the Array .find function on the users collection to find and return the selected user object.
8th May 2019, 3:09 PM
Samuel Nnaji
0
Thats how the question goes
8th May 2019, 3:13 PM
Samuel Nnaji
0
Fill in the blanks to declare an arrow function that takes an array and prints the odd elements. => arr el
7th Jun 2020, 12:51 AM
Frederick John Suerte
Frederick John Suerte - avatar
0
I posted one answer Trust me
28th Jun 2020, 8:34 AM
Faith Mundi
Faith Mundi - avatar
- 1
What's the answer for this question in JavaScript?? Fill in the blanks to declare an arrow function that takes an array and prints the odd elements. const printOdds = (arr) __ { ___.forEach( __ => { if (el % 2 != 0) console.log(el); }); }
14th May 2020, 2:51 PM
haneen