map, filter & reduce

Run Settings
LanguageJavaScript
Language Version
Run Command
//ES6 let list = ['Bob', 'Tom', 'Geoffrey', 'Scotty']; //Filter for names less than 7 chars long, make uppercase and put into object {id, name, length} var nameArray = list.filter( x => x.length < 7 ) // [ 'Bob', 'Tom', 'Scotty' ] .map( z => z.toUpperCase()) // [ 'BOB', 'TOM', 'SCOTTY' ] .reduce( (current, next, i) => { // next is the next item in [ 'BOB', 'TOM', 'SCOTTY' ] staring at the beginning current.push({ id: i, name: next, length: next.length }); return current; // current will return when we have gone through each item in [ 'BOB', 'TOM', 'SCOTTY' ] }, []); //[] is the 'current' starting item console.log(nameArray);
//ES5 chaining let list = ['Bob', 'Tom', 'Geoffrey', 'Scotty']; var nameArray = list.filter( function(x) { return x.length < 7 }) .map( function(z) { return z.toUpperCase()}) .reduce( function(current, next, i) { current.push({ id: i, name: next, length: next.length }); return current; }, []); console.log(nameArray);
//ES5 non-chaing let list = ['Bob', 'Tom', 'Geoffrey', 'Scotty']; var lessThan7Names = list.filter( function(x) { return x.length < 7 }); // [ 'Bob', 'Tom', 'Scotty' ] var upperCaseNames = lessThan7Names.map( function(z) { return z.toUpperCase()}); // [ 'BOB', 'TOM', 'SCOTTY' ] var arrayOfObjectsWithInfo = upperCaseNames.reduce( function(current, next, i) { current.push({ id: i, name: next, length: next.length }); return current; }, []); console.log(arrayOfObjectsWithInfo);
Editor Settings
Theme
Key bindings
Full width
Lines