.forEach iteration in ES6 | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Respostas
+ 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