- #">Ascend and Descend #
- https://ramdajs.com/docs/#path 加强版的prop">https://ramdajs.com/docs/#path 加强版的prop
- https://ramdajs.com/docs/#nth 取出list的某一项">https://ramdajs.com/docs/#nth 取出list的某一项
- https://ramdajs.com/docs/#comparator 兼容ie">https://ramdajs.com/docs/#comparator 兼容ie
Comparators turn any comparing function into a sorting function, without fretting browser inconsistencies. (5 min. read)
比较器将任何比较函数转换为排序函数,而不会影响浏览器的不一致性(5分钟(阅读)
Ascend and Descend #
You won’t need these as often as the main sorting functions, but they can be useful in some scenarios.
The functions we just saw, ascend/descend, are comparators. They take a function to apply against each value.
Here’s a sort by height.
import { ascend, sort } from 'ramda';
const people = [{
height: 23
}, {
height: 230
}, {
height: 2.3
}];
const getHeight = (x) => x.height;
const byHeight = ascend(getHeight);
const result = sort(byHeight, people);
console.log({ result });
{ result: [ { height: 2.3 }, { height: 23 }, { height: 230 } ] }
Ramda’s prop function can replace the getHeight function.
import { ascend, prop, sort } from 'ramda';
const people = [{
height: 23
}, {
height: 230
}, {
height: 2.3
}];
const byHeight = ascend(prop('height'));
const result = sort(byHeight, people);
console.log({ result });
{ result: [ { height: 2.3 }, { height: 23 }, { height: 230 } ] }
And path lets you easily compare things, no matter how nested their properties are.
import { ascend, path, sort } from 'ramda';
const people = [{
name: 'I am second',
metadata: {
attributes: {
height: {
value: 23
}
}
}
}, {
name: 'I am last',
metadata: {
attributes: {
height: {
value: 230
}
}
}
}, {
name: 'I am first',
metadata: {
attributes: {
height: {
value: 2.3
}
}
}
}];
const getHeight = path(['metadata', 'attributes', 'height', 'value']);
const byHeight = ascend(getHeight);
const result = sort(byHeight, people);
console.log(result);
[ { name: 'I am first', metadata: { attributes: [Object] } },
{ name: 'I am second', metadata: { attributes: [Object] } },
{ name: 'I am last', metadata: { attributes: [Object] } } ]
https://ramdajs.com/docs/#path 加强版的prop
path Object 取出对象的某个属性
[Idx] → {a} → a | Undefined
Idx = String | Int
Parameters
Added in v0.2.0
Retrieve the value at a given path.
See also prop, nth.
R.path(['a', 'b'], {a: {b: 2}}); //=> 2
R.path(['a', 'b'], {c: {b: 2}}); //=> undefined
R.path(['a', 'b', 0], {a: {b: [1, 2, 3]}}); //=> 1
R.path(['a', 'b', -2], {a: {b: [1, 2, 3]}}); //=> 2
https://ramdajs.com/docs/#nth 取出list的某一项
nth
Number → [a] → a | Undefined
Number → String → String
Parameters
Added in v0.1.0
Returns the nth element of the given list or string. If n is negative the element at index length + n is returned.
const list = ['foo', 'bar', 'baz', 'quux'];
R.nth(1, list); //=> 'bar'
R.nth(-1, list); //=> 'quux'
R.nth(-99, list); //=> undefined
R.nth(2, 'abc'); //=> 'c'
R.nth(3, 'abc'); //=> ''
Lower-Level Comparator #
Ramda also has a comparator function. It creates comparator functions out of regular functions that compare two elements.
This function, for example…
低电平比较器#
拉姆达还有一个比较器功能。
它从比较两个元素的常规函数中创建比较器函数。
这个函数,例如…
const byHeight = (a, b) => a.height > b.height;
…works with sort in modern browsers.
import { sort } from 'ramda';
const people = [{ height: 20 }, { height: 10 }];
const byHeight = (a, b) => a.height > b.height;
const result = sort(byHeight, people);
console.log({ result });
{ result: [ { height: 10 }, { height: 20 } ] }
But since byHeight returns a boolean, older browsers like Internet Explorer won’t sort people correctly. Older browsers require your comparator to return a number!
See this StackOverflow answer for more details.
A proper comparator looks like this
但由于byHeight返回一个布尔值,像InternetExplorer这样的旧浏览器无法正确地对人进行排序。
较旧的浏览器要求您的比较器返回一个数字!
有关更多详细信息,请参阅此答案。 一个合适的比较器如下所示
const byHeight = (a, b) => {
if (a.height > b.height) {
return 1;
}
if (a.height < b.height) {
return -1;
}
return 0;
};
Allowing this sort to work everywhere.
import { sort } from 'ramda';
const people = [{ height: 20 }, { height: 10 }];
const byHeight = (a, b) => {
if (a.height > b.height) {
return 1;
}
if (a.height < b.height) {
return -1;
}
return 0;
};
const result = sort(byHeight, people);
console.log({ result });
A bit verbose. Fortunately, Ramda turns any comparison function into a proper comparator. All we need is the comparator function.
Take your original byHeight function and flip > to <…
有点冗长。
幸运的是,Ramda将任何比较函数转换为适当的比较器。
我们所需要的只是比较器函数。
使用原始的byHeight功能,
然后将>翻转到<…
const byHeight = (a, b) => a.height < b.height;
And wrap it in comparator. I’d prefer renaming it so the comparator function can be named byHeight. 然后用比较器把它包起来。我更喜欢将其重命名,以便比较器函数可以按高度命名。
// rename this
const compareHeights = (a, b) => a.height < b.height;
// giving this a proper name
const byHeight = comparator(compareHeights)
Now use it.
import { comparator, sort } from 'ramda';
const people = [{ height: 20 }, { height: 10 }];
const compareHeights = (a, b) => a.height < b.height;
const byHeight = comparator(compareHeights);
const result = sort(byHeight, people);
console.log({ result });
https://ramdajs.com/docs/#comparator 兼容ie
((a, b) → Boolean) → ((a, b) → Number)
Parameters
Added in v0.1.0
Makes a comparator function out of a function that reports whether the first element is less than the second.
((a,b)→ 布尔值)→ ((a、 (b)→ (数字) 参数 添加到v0.1.0中 从报告第一个元素是否小于第二个元素的函数中生成比较器函数。
const byAge = R.comparator((a, b) => a.age < b.age);
const people = [
{ name: 'Emma', age: 70 },
{ name: 'Peter', age: 78 },
{ name: 'Mikhail', age: 62 },
];
const peopleByIncreasingAge = R.sort(byAge, people);
//=> [{ name: 'Mikhail', age: 62 },{ name: 'Emma', age: 70 }, { name: 'Peter', age: 78 }]