Code snippets with gifs, to avoid copy and paste the code and write it all.
Example:
const array = ['this', 'is', 'a', 'test'];
array.forEach((item, index) => {
});
Gif:
Example:
var pilots = [
{
id: 10,
name: "Poe Dameron",
years: 14,
},
{
id: 41,
name: "Tallissan Lintra",
years: 16,
},
];
const totalYears = pilots.reduce((acc, pilot) => acc + pilot.years, 0);
Gif:
Example:
const fifteen = inventors.filter(function(inventor) {
if(inventor.year >= 1500 && inventor.year < 1600) {
return true;
}
});
const fifteen = inventors.filter(inventor => (
inventor.year >= 1500 && inventor.year < 1600
));
Example:
const fullNames = inventors.map(inventor => (
`${inventor.first} ${inventor.last}`
));
Example:
const ordered = inventors.sort(function(a, b) {
if(a.year > b.year) {
return 1;
} else {
return -1;
}
});
const ordered = inventors.sort((a, b) => a.year > b.year ? 1 : -1);
Note: Examples from JavaScript 30 from Mike Ekkel
There is a file for each functionality, to be able to execute it.
Example:
-
File es6/forEach/forEach.js
- Make it executable:
chmod +x es6/forEach/forEach.js
- Run it:
./es6/forEach/forEach.js