+ 3
.forEach iteration in ES6
How do i iterate an array .forEach function and use it to populate a select element with options
20 ответов
+ 8
Any sample input and their respective outputs? Your question is not very clear.
+ 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)
+ 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>` );
+ 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
+ 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
+ 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.
+ 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.
+ 3
So this thread is solved with Calviղ's first linked code, correct?
+ 3
Is this homework?
+ 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".
🤷🏻♂️
+ 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
+ 1
Thanks for the replies but the requirement is to use a forEach function and pls using ES6 syntax no jquery
+ 1
Yes kind of a test
+ 1
JavaScript
ES 6 (ECMAScript6)
Loops and Function in ES6
const printOdds = (arr) => {
arr.forEach(el=>{
if (el % 2 != 0) console.log(el);
});
}
+ 1
const printOdds = (arr)
=>
{
arr
.forEach(
el
=> {
if (el % 2 != 0) console.log(el);
});
}
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.
0
Thats how the question goes
0
Fill in the blanks to declare an arrow function that takes an array and prints the odd elements.
=>
arr
el
0
I posted one answer
Trust me
- 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);
});
}