Created
March 4, 2017 21:37
-
-
Save krazov/d44fb95fae573d66c135663f7c3a5941 to your computer and use it in GitHub Desktop.
Iterations versus steps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the array | |
const array = [ | |
{ value: 1 }, | |
{ value: 2 }, | |
{ value: 3 }, | |
{ value: 4 }, | |
{ value: 5 }, | |
{ value: 6 }, | |
{ value: 7 }, | |
{ value: 8 }, | |
{ value: 9 }, | |
{ value: 10 } | |
]; | |
// the old way | |
let newArray = []; | |
for (let i = 0; i < array.length; i++) { | |
if (array[i].value % 2 == 0) { | |
newArray.push({ newValue: array[i].value * 2 }); | |
} | |
} | |
// the new way | |
const newerArray = array | |
.map(object => object.value) | |
.filter(value => value % 2 == 0) | |
.map(value => value * 2) | |
.map(doubledValue => ({ newValue: doubledValue})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment