.forEach:
.forEach()
, is used to execute the same code on every element in an array but does not change the array and it returns undefined.
In the below example, we would use .forEach()
to iterate over an array of technology and log that we would want to learn each of them.
1 2 3 |
let technology = ['nodejs','angular','javscript','jquery']; technology.forEach(function(learntechnology){ console.log('I want to learn '+learntechnology); }); |
.map():
.map()
executes the same code on every element in an array and returns a new array with the updated elements.
In the below example we would use map
iterate over the elements of the cost
array and divide each element by 10, then assign our new array containing the new cost to the variable newCost
.
1 2 3 4 |
let cost = [100,400,300,700]; let newCost = cost.map(function(costItem){ return costItem / 10; }); console.log(newCost); |
.filter():
.filter()
checks every element in an array to see if it meets a certain criteria and returns a new array with the elements that return truthy
for the criteria.
1 2 3 4 |
let cost = [100,400,50,40,700]; let smallCost = cost.filter(function(costItem){ return costItem < 200 }); console.log(smallCost); |
Hope it helps
Recent Comments