Javascript 从ES5到ES7 - 19 map,reduce,filter

1. map
const numbers = [2, 4, 8, 10];
const halves = numbers.map(x => x / 2);

2. filter
const words = ["spray", "limit", "elite", "exuberant", "destruction", "present"];

const longWords = words.filter(word => word.length > 6);
// longWords is ["exuberant", "destruction", "present"]

3. reduce
const total = [0, 1, 2, 3].reduce((sum, value) => sum + value, 1);


4. chain

const words = ['ab', 'abc', 'bcde'];
words.filter( w => w.startsWith('a') )
    .map( w => w.length )
    .reduce( (acc, v) => Math.max( acc, v ), 0 );
	

 

你可能感兴趣的:(Javascript)