JeffLearnsReact – Iterators

Iterators iterate myArray = [‘one’, ‘two’, ‘three’]; .forEach() myArray.forEach(function(myArrayItem) { console.log(myArrayItem); }); myArray.forEach((myArrayItem) => { console.log(myArrayItem); }); .map() Iterates and CHANGES the contents of an array, maps it to another array let numbers = [1, 2, 3, 4, 5]; let bigNumbers = numbers.map(function(number) { return number * 10; }); let numbers = [1, 2, 3, […]

Iterators iterate

myArray = [‘one’, ‘two’, ‘three’];

.forEach()

myArray.forEach(function(myArrayItem) {
console.log(myArrayItem);
});

myArray.forEach((myArrayItem) => {
console.log(myArrayItem);
});

.map()

Iterates and CHANGES the contents of an array, maps it to another array

let numbers = [1, 2, 3, 4, 5];

let bigNumbers = numbers.map(function(number) {
return number * 10;
});
let numbers = [1, 2, 3, 4, 5];
let bigNumbers = numbers.map(numbers => numbers * 10);

.filter()

Creates a new array like .map() but evaluates each item based on a condition

let words = [‘chair’, ‘music’, ‘pillow’, ‘brick’, ‘pen’, ‘door’];

let shortWords = words.filter(function(word) {
return word.length < 6;
});
let shortWords = words.filter(word => word.length < 4);

Powered by WPeMatico