Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 4 years ago.
Lets say I have:
var someValues = [1, 'abc', 3, 'sss'];
How can I use an arrow function to loop through each and perform an operation on each value?
In short:
someValues.forEach((element) => {
console.log(element);
});
If you care about index, then second parameter can be passed to receive the index of current element:
someValues.forEach((element, index) => {
console.log(`Current index: ${index}`);
console.log(element);
});
Refer here to know more about Array of ES6: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
One statement can be written as such:
someValues.forEach(x => console.log(x));
or multiple statements can be enclosed in {}
like this:
someValues.forEach(x => { let a = 2 + x; console.log(a); });