Skip to content

Instantly share code, notes, and snippets.

@krazov
Created March 4, 2017 21:37
Show Gist options
  • Save krazov/d44fb95fae573d66c135663f7c3a5941 to your computer and use it in GitHub Desktop.
Save krazov/d44fb95fae573d66c135663f7c3a5941 to your computer and use it in GitHub Desktop.
Iterations versus steps
// 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