0% found this document useful (0 votes)
59 views3 pages

JS Es6

This document discusses JavaScript array helper methods - forEach(), map(), and filter(). It provides examples of using each method to iterate through arrays and perform actions on the array elements. forEach() iterates over elements and allows side effects, map() maps each element to a new value and returns a new array, and filter() filters elements based on a condition and returns a new array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

JS Es6

This document discusses JavaScript array helper methods - forEach(), map(), and filter(). It provides examples of using each method to iterate through arrays and perform actions on the array elements. forEach() iterates over elements and allows side effects, map() maps each element to a new value and returns a new array, and filter() filters elements based on a condition and returns a new array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Javascript ES6

Array Helpers
foreach()

Intro

var myArray = ['firstItem', 'secondItem', 'thirdItem'];


myArray.forEach(function(item) {
console.log(item);
});
/*
Output (in console):
firstItem
secondItem
thirdItem
*/

Example: sum of numbers

var numbers = [4, 5, 18, 3, 21, 25, 9];


var sum = 0;
numbers.forEach(function(number) {
sum += number;
});
console.log(sum);
/*
Output (in console):
85
*/

map()

Intro

var nums = [1, 2, 3, 4];


var doubledNums = nums.map(function(num) {
return num*2;
});
console.log(doubledNums);
/*

1/3
Output (in console):
[2, 4, 6, 8]
*/

Example: prices of cars

var cars = [
{model : 'Buick', price: '10k'},
{model : 'Camaro', price: '40k'}
];
var prices = cars.map(function(car) {
return car.price;
});
console.log(prices);
/*
Output (in console):
['10k', '40k']
*/

filter()

Intro (Example: grocer y shop products)

var products = [
{name: 'cucumber', type: 'vegetable'},
{name: 'celery', type: 'vegetable'},
{name: 'banana', type: 'fruit'},
{name: 'tomato', type: 'fruit'}
];
var fruits = products.filter(function(product) {
return product.type === 'fruit';
});
console.log(fruits);
/*
Output (in console):
[{"name":"banana","type":"fruit"},
{"name":"tomato","type":"fruit"}]
*/

Example: grocer y shop products (continued)

var products = [
{name: 'cucumber', type: 'vegetable', price: 1 },

2/3
{name: 'celery', type: 'vegetable', price: 3},
{name: 'banana', type: 'fruit', price: 2},
{name: 'tomato', type: 'fruit' price: 2}
];
var affordableVegs = products.filter(function(product) {
return product.type === 'vegetable' && product.price < 3;
});
console.log(affordableVegs);
/*
Output (in console):
[{"name":"banana","type":"fruit","price":1}]
*/

3/3

You might also like