24
loading...
This website collects cookies to deliver better user experience
map()
iterates over an array. The difference is, map will not change (or mutate) the original array. Map always returns a new array. Let's take an example function that capitalizes all items in a list and implement it with for, forEach, and map:const list = ['apple', 'banana', 'carrot'];
const capitalized = capitalizeList(list);
function capitalizeList(list) {
for (let i = 0; i < list.length; i++) {
list[i] = list[i].toUpperCase();
}
return list;
}
function capitalizeList(list) {
let newList = [];
list.forEach(item => {
newList.push(item.toUpperCase());
});
return newList;
}
function capitalizeList(list) {
return list.map(item => item.toUpperCase());
}
map()
, filter()
and reduce()
will also not change the original array.function filterByLetter(list, letter) {
for (let i = 0; i < list.length; i++) {
if (!list[i].startsWith(letter)) {
list.splice(i, 1);
}
}
return list;
}
function filterByLetter(list, letter) {
return list.filter(item => item.startsWith(letter));
}
function sumNumbers(numbers) {
let sum = 0;
for (let i = 0; i < numbers; i++) {
sum += numbers[i];
}
return sum;
}
function sumNumbers(numbers) {
return numbers.reduce((a, b) => a + b, 0);
}
Pure Function | What's it for? |
---|---|
[].map() | Returning a new array of new items |
[].filter() | Filtering arrays |
[].reduce() | Morphing arrays into new data structures |
[].find() | Finding first occurrence of item |
[].some() | Checking if array has at least one item matching criteria |
[].includes() | Checking if array has at least one item matching raw param value |
[].every() | Checking if array has ALL items matching criteria |
[].slice(start, end) | Trims array at positions |
[].concat() | Merging two arrays together |
[].join() | Converting array to a single string |
[].flatMap() | Converting a 2D-array into a single array |
Impure Function | What's it for? |
---|---|
[].push() | Adding to an array |
[].pop() | Removing an item from array |
[].sort() | Sorting |
[].shift() | Removing first item in array |
[].unshift() | Adding items to the beginning of the array |
[].splice() | Removing/replacing items in array |
[].reverse() | Reversing the order |
var
and replaced it with let
and const
in 2016. If you've ditched var, you're already on the right track. let
(and var) allows you to reassign variables:let vegetable = 'asparagus';
vegetable = 'broccoli'; // valid JavaScript
var carb = 'rice';
carb = 'bread'; // valid JavaScript
var carb = 'beans'; // also valid JavaScript
const
will not allow you to reassign variablesconst legume = 'chickpea';
legume = 'soybean'; // syntax error
let
is present in the impure examples. If you program only with const
, you're forced to write more pure functions.const snacks = {
healthyOption: '',
unhealthyOption: 'Cookies'
}
const addHealthyOption = (snacks, healthyOption) => {
snacks.healthyOption = healthyOption;
return snacks;
}
const newSnackObject = addHealthyOption(snacks, 'Edamame');
console.log(newSnackObject) // 😊 { healthyOption: 'Edamame', unhealthyOption: 'Cookies' }
console.log(snacks.healthyOption) // 😦 'Edamame'
const addHealthyOption = (snacks, healthyOption) => {
return {...snacks, healthyOption}
}
const newSnackObject = addHealthyOption(snacks, 'Edamame');
console.log(newSnackObject) // 😊 { healthyOption: 'Edamame', unhealthyOption: 'Cookies' }
console.log(snacks.healthyOption) // 😊 ''
const snacks = {
healthyOptions: [],
unhealthyOptions: ['Cookies']
}
const addHealthyOptions = (snacks, healthyOptions) => {
snacks.healthyOptions.push(healthyOptions);
return snacks;
}
const newSnackObject = addHealthyOptions(snacks, ['Edamame', 'Hummus and Veggies']);
console.log(newSnackObject) // 😊 { healthyOptions: ['Edamame', 'Hummus and Veggies'], unhealthyOptions: ['Cookies'] }
console.log(snacks.healthyOptions) // 😦 ['Edamame', 'Hummus and Veggies']
const snacks = {
healthyOptions: [],
unhealthyOptions: ['Cookies']
}
const addHealthyOptions = (snacks, healthyOptions) => {
return {
...snacks,
healthyOptions: [...snacks.healthyOptions, healthyOptions]
}
}
const newSnackObject = addHealthyOptions(snacks, ['Edamame', 'Hummus and Veggies']);
console.log(newSnackObject) // 😊 { healthyOptions: ['Edamame', 'Hummus and Veggies'], unhealthyOptions: ['Cookies'] }
console.log(snacks.healthyOptions) // 😊 []