This code returns everyone’s age. Refactor it to be point-free.
https://ramdajs.com/docs/#map
处理list(可能是数组,也可能是对象)
Functor f => (a → b) → f a → f b
Parameters
Added in v0.1.0
Takes a function and a functor, applies the function to each of the functor’s values, and returns a functor of the same shape.
Ramda provides suitable map implementations for Array and Object, so this function may be applied to [1, 2, 3] or {x: 1, y: 2, z: 3}.
Dispatches to the map method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
Also treats functions as functors and will compose them together.
See also transduce, addIndex.
const double = x => x * 2;
R.map(double, [1, 2, 3]); //=> [2, 4, 6]
R.map(double, {x: 1, y: 2, z: 3}); //=> {x: 2, y: 4, z: 6}
https://ramdajs.com/docs/#prop
处理对象 获取对象的属性
Idx → {s: a} → a | Undefined
Idx = String | Int
Parameters
Added in v0.1.0
Returns a function that when supplied an object returns the indicated property of that object, if it exists.
See also path, nth.
R.prop('x', {x: 100}); //=> 100
R.prop('x', {}); //=> undefined
R.prop(0, [100]); //=> 100
R.compose(R.inc, R.prop('x'))({ x: 3 }) //=> 4
https://ramdajs.com/docs/#pluck 拔掉值
处理list(可能是数组,也可能是对象)
Functor f => k → f {k: v} → f v
Parameters
- key The key name to pluck off of each object.
- f The array or functor to consider.
ReturnsArrayThe list of values for the given key.
Added in v0.1.0
Returns a new list by plucking the same named property off all objects in the list supplied.
pluck will work on any functor in addition to arrays, as it is equivalent to R.map(R.prop(k), f).
See also props.
var getAges = R.pluck('age');
getAges([{name: 'fred', age: 29}, {name: 'wilma', age: 27}]); //=> [29, 27]
R.pluck(0, [[1, 2], [3, 4]]); //=> [1, 3]
R.pluck('val', {a: {val: 3}, b: {val: 5}}); //=> {a: 3, b: 5}
拔掉list的某个属性
// import R from 'ramda';
const getAges = (people) => people.map((person) => person.age);
[{“age”:20},{“age”:39},{“age”:14},{“age”:33},{“age”:60},{“age”:45}] | [20,39,14,33,60,45] |
---|---|
import { map, pluck, prop } from 'ramda';
// You may have done this
const getAges1 = map(prop('age'));
// And you'd be correct :D
// But this works too
const getAges2 = pluck('age');