Quick actions

cmd+k|ctrl+k

Navigation

Languages

map, filter & reduce

Snippet info

Language

JavaScript

Visibility

public

Author

peter

Created

2016-05-15T04:52:21Z

Updated

2016-05-15T04:52:21Z

//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);
INFO