1. lodash中的pick方法 https://lodash.com/docs/4.17.10#pick
_.pick(object, [paths])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 var _ = require ("lodash" );var objects = [{ "user" : "a" , "age" : 20 , "name" : "aname" }, { "user" : "b" , "age" : 30 , "name" : "bname" }, { "user" : "c" , "age" : 40 , "name" : "cname" }]; var results = [];console .log(objects.filter(function (object ) { var a = _.pick(object, ['user' , 'name' ]); console .log(a); results.push(a); })); console .log(results);
加复杂条件用 _.pickBy()
2. ES6 where筛选 1 2 3 4 5 6 7 8 9 10 var _ = require ("underscore" );var json = '[{"user": "a", "age": 20}, {"user": "b", "age": 30}, {"user": "c", "age": 40}]' ;var users = JSON .parse(json);var filtered = _.where(users, { user: "a" }); console .log(filtered);
https://github.com/lodash/lodash/issues/1247
3. Quickly filter an object by keys https://codereview.stackexchange.com/questions/79384/quickly-filter-an-object-by-keys
1 2 3 4 5 var matchingKey = myKeys.indexOf(myString) !== -1 var matchingKeys = myKeys.filter(function (key ) { return key.indexOf(myString) !== -1 });var matchingValues = matchingKeys .map(function (key ) { return myObject[key] });
4. 自己写的es6方式 解构+合并 destructuring and shorthand object literals
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 var objects = [{ "user" : "a" , "age" : 20 , "name" : "aname" }, { "user" : "b" , "age" : 30 , "name" : "bname" }, { "user" : "c" , "age" : 40 , "name" : "cname" }]; function jsonfilter (json ) { const { user,name} = json; return { user,name}; } var args = [];objects.map((object ) => { args = args.concat(jsonfilter(object)); }); console .log(args);
You don’t (may not) need Lodash/Underscore You don’t (may not) need Lodash/Underscore
常用举例10 Lodash Features You Can Replace with ES6
1. Map, Filter, Reduce 搭配箭头函数
1 2 3 4 5 6 7 8 9 10 11 12 _.map([1 , 2 , 3 ], function (n ) { return n * 3 ; }); _.reduce([1 , 2 , 3 ], function (total, n ) { return total + n; }, 0 ); _.filter([1 , 2 , 3 ], function (n ) { return n <= 2 ; }); [1 , 2 , 3 ].map(n => n * 3 ); [1 , 2 , 3 ].reduce((total, n ) => total + n); [1 , 2 , 3 ].filter(n => n <= 2 );
2. Head & Tail ES6数组解构获取头尾
1 2 3 4 5 6 7 _.head([1 , 2 , 3 , 4 ]); _.tail([1 , 2 , 3 , 4 ]); const [head, ...tail] = [1 , 2 , 3 ];
1 2 3 4 5 6 7 8 9 10 11 12 13 _.initial([1 , 2 , 3 ]); _.last([1 , 2 , 3 ]); const [last, ...initial] = [1 , 2 , 3 ].reverse();const xs = [1 , 2 , 3 ];const [last, ...initial] = [...xs].reverse();
3. Rest and Spread Rest和Spread允许我们定义和调用接受一个可变数量的参数的函数。
https://lodash.com/docs/4.17.10#rest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 var say = _.rest(function (what, names ) { var last = _.last(names); var initial = _.initial(names); var finalSeparator = (_.size(names) > 1 ? ', & ' : '' ); return what + ' ' + initial.join(', ' ) + finalSeparator + _.last(names); }); say('hello' , 'fred' , 'barney' , 'pebbles' ); const say = (what, ...names ) => { const [last, ...initial] = names.reverse(); const finalSeparator = (names.length > 1 ? ', &' : '' ); return `${what} ${initial.join(', ' )} ${finalSeparator} ${last} ` ; }; say('hello' , 'fred' , 'barney' , 'pebbles' );
4. Curry 函数柯里化是指:把接受多个参数的函数转换成接受一个单一参数的函数
1 2 3 4 5 6 7 8 9 10 11 12 13 function add (a, b ) { return a + b; } var curriedAdd = _.curry(add);var add2 = curriedAdd(2 );add2(1 ); const add = a => b => a + b;const add2 = add(2 );add2(1 );
方便debug1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 var lodashAdd = _.curry(function (a, b ) { return a + b; }); var add3 = lodashAdd(3 );console .log(add3.length)console .log(add3);const es6Add = a => b => a + b;const add3 = es6Add(3 );console .log(add3.length);console .log(add3);
5. Partial 分步输入实参
1 2 3 4 5 6 7 8 9 10 11 12 var greet = function (greeting, name ) { return greeting + ' ' + name; }; var sayHelloTo = _.partial(greet, 'hello' );sayHelloTo('fred' ); const sayHelloTo = name => greet('hello' , name);sayHelloTo('fred' );
use rest parameters with the spread operator to partially apply variadic functions:
1 2 3 const sayHelloTo = (name, ...args ) => greet('hello' , name, ...args);sayHelloTo('fred' , 1 , 2 , 3 );
6. Operators 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 _.eq(3 , 3 ); _.add(10 , 1 ); _.map([1 , 2 , 3 ], function (n ) { return _.multiply(n, 10 ); }); _.reduce([1 , 2 , 3 ], _.add); 3 === 3 10 + 1 [1 , 2 , 3 ].map(n => n * 10 ); [1 , 2 , 3 ].reduce((total, n ) => total + n);
7. Path 返回Object对象中对应的path路径的值的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 var object = { 'a' : [{ 'b' : { 'c' : 3 } }, 4 ] };_.at(object, ['a[0].b.c' , 'a[1]' ]); _.at(['a' , 'b' , 'c' ], 0 , 2 ); [ obj => obj.a[0 ].b.c, obj => obj.a[1 ] ].map(path => path(object)); [ arr => arr[0 ], arr => arr[2 ] ].map(path => path(['a' , 'b' , 'c' ]));
8. Pick 1 2 3 4 5 6 7 8 9 10 var object = { 'a' : 1 , 'b' : '2' , 'c' : 3 };return _.pick(object, ['a' , 'c' ]);const { a, c } = { a : 1 , b : 2 , c : 3 };return { a, c };
9. Constant, Identity, Noop 1 2 3 4 5 6 _.constant({ 'a' : 1 })(); _.identity({ user : 'fred' }); _.noop();
使用箭头函数1 2 3 4 5 6 7 8 9 10 11 12 13 const constant = x => () => x;const identity = x => x;const noop = () => undefined ;======= (() => ({ a : 1 }))(); (x => x)({ user : 'fred' }); (() => undefined )();
10. Chaining & Flow 替代链式写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 _([1 , 2 , 3 ]) .tap(function (array ) { array.pop(); }) .reverse() .value(); const pipeline = [ array => { array.pop(); return array; }, array => array.reverse() ]; pipeline.reduce((xs, f ) => f(xs), [1 , 2 , 3 ]);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 const pipe = functions => data => { return functions.reduce( (value, func) => func(value), data ); }; const pipeline = pipe([ x => x * 2 , x => x / 3 , x => x > 5 , b => !b ]); pipeline(5 ); pipeline(20 );