ZetCode

Ramda 教程

最后修改于 2023 年 10 月 18 日

在本文中,我们将展示如何使用 Ramda 库,该库提供了在 JavaScript 中进行高级函数式编程的工具。 在本文中,我们互换使用列表和数组这两个术语。

Ramda

Ramda 是一个实用的函数式库,适用于 JavaScript 程序员。该库专注于不变性和无副作用函数。 Ramda 函数也自动柯里化,这允许仅通过不提供最终参数来从旧函数构建新函数。

在本文中,我们将在 Node 应用程序中使用 Ramda。

设置 Ramda

首先,我们安装 Ramda。

$ npm init -y

我们启动一个新的 Node 应用程序。

$ npm i ramda

我们使用 npm i ramda 命令安装 Ramda。

Ramda add、subtract 函数

add 函数将两个值相加,而 subtract 函数将两个值相减。

add_sub.js
import * as R from 'ramda';

console.log(R.add(2, 5));
console.log(R.subtract(2, 5));

let res = R.add(R.add(2, 5), R.subtract(2, 10));
console.log(res);

此示例使用 addsubtract 两个函数。

let res = R.add(R.add(2, 5), R.subtract(2, 10));

这里我们结合了这些函数。

$ node add_sub.js
7
-3
-1

Ramda flip 函数

flip 函数从提供的函数返回一个新函数,其中其参数已颠倒。

flipfun.js
import * as R from 'ramda';

let val = R.subtract(2, 10);
console.log(val);

let val2 = R.flip(R.subtract)(2, 10);
console.log(val2);

此示例使用 flip 颠倒了 subtract 函数的参数。

$ node flipfun.js
-8
8

Ramda call 函数

call 函数在由逗号分隔的参数上调用提供的函数。

calling.js
import * as R from 'ramda';

let res = R.call(R.add, 1, 2);
console.log(res);

console.log(R.call(R.repeat, 'x')(5));

R.call(console.log, [1, 2, 3]);

此示例使用 call 函数。

let res = R.call(R.add, 1, 2);

我们调用 add 函数来添加两个整数。

console.log(R.call(R.repeat, 'x')(5));

我们调用 repeat 函数来生成一个包含五个“x”字母的列表。

R.call(console.log, [1, 2, 3]);

最后,我们使用 call 函数输出列表。

$ node calling.js
3
[ 'x', 'x', 'x', 'x', 'x' ]
[ 1, 2, 3 ]

Ramda apply 函数

apply 函数在一列参数上调用提供的函数。

applyfun.js
import * as R from 'ramda';

let nums = [3, 5, 7, 8, 2, 1];

let res = R.apply(Math.min, nums);
console.log(res);

let res2 = R.apply(Math.max, nums);
console.log(res2);

此示例使用 apply 函数来计算最小值和最大值。

let res = R.apply(Math.min, nums);

我们在 nums 列表上调用 Math.min 函数。我们从这些值中得到最小值。

$ node applyfun.js
1
8

我们得到最小值和最大值。

Ramda 自动柯里化

柯里化是将需要多个参数的函数转换为另一个函数的过程,该函数在提供较少参数时返回一个新函数,该函数等待剩余的参数。

currying.js
import * as R from 'ramda';

let addOneToAll = R.map(R.add(1));
let res = addOneToAll([1,2,3]);

console.log(res);

在此示例中,我们创建了一个 addOneToAll 函数,该函数将列表中每个元素递增 1。

$ node currying.js
[ 2, 3, 4 ]

Ramda head、tail、init、last 函数

head 返回给定列表或字符串的第一个元素。 tail 返回给定列表或字符串中除第一个元素之外的所有元素。 init 返回给定列表或字符串中除最后一个元素之外的所有元素。 last 返回给定列表或字符串的最后一个元素。

head_tail.js
import * as R from 'ramda';

let nums = [2, 4, 6, 8, 10];

console.log(R.head(nums));
console.log(R.tail(nums));
console.log(R.init(nums));
console.log(R.last(nums));

此示例将 headtailinitlast 函数应用于一个值数组。

$ node head_tail.js
2
[ 4, 6, 8, 10 ]
[ 2, 4, 6, 8 ]
10

Ramda length 函数

length 函数返回列表中元素的数量。

lengthfun.js
import * as R from 'ramda';

let nums = [1, 2, 2, 2, 3, 3, 4, 5, 5, 5, 6, 7];

let n1 = R.length(nums);
console.log(n1);

let n2 = R.length(R.uniq(nums));
console.log(n2);

在此示例中,我们计算列表中元素的数量以及列表中唯一元素的数量。

$ node lengthfn.js
12
7

列表中有十二个元素,列表中有七个唯一元素。

Ramda prop 函数

prop 函数返回对象的指定属性(如果它存在)。

propfun.js
import * as R from 'ramda';

console.log(R.prop('name', { name: 'John', age: 25 }));
console.log(R.prop('age', { name: 'John', age: 25 }));

使用 prop 函数,我们获取 nameage 属性的值。

$ node propfun.js
John
25

Ramda pluck 函数

pluck 函数通过从提供的列表中提取指定属性来返回一个新列表。

plucking.js
import * as R from 'ramda';

const users = [
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Andrew', age: 43 },
  { name: 'Peter', age: 81 },
  { name: 'Anna', age: 43 },
  { name: 'Albert', age: 76 },
  { name: 'Adam', age: 47 },
  { name: 'Robert', age: 72 }
];

console.log(R.pluck('age', users));
console.log(R.pluck('name', users));

使用 pluck 函数,我们获取 nameage 属性并形成两个新列表。

$ node plucking.js
[ 25, 51, 43, 81, 43, 76, 47, 72 ]
[ 'John',
  'Lenny',
  'Andrew',
  'Peter',
  'Anna',
  'Albert',
  'Adam',
  'Robert' ]

在以下示例中,我们使用形成的列表。

plucking2.js
import * as R from 'ramda';

const users = [
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Andrew', age: 43 },
  { name: 'Peter', age: 81 },
  { name: 'Anna', age: 43 },
  { name: 'Albert', age: 76 },
  { name: 'Adam', age: 47 },
  { name: 'Robert', age: 72 }
];

let maxAge = R.apply(Math.max, R.pluck('age', users));
// let maxAge = Math.max(... R.pluck('age', users));

console.log(`The oldest person is ${maxAge} years old.`);

在此示例中,我们找出某人的最大年龄。

let maxAge = R.apply(Math.max, R.pluck('age', users));

通过在年龄列表中调用 Math.max 函数,我们得到最大年龄。

// let maxAge = Math.max(... R.pluck('age', users));

另一种注释解决方案使用扩展运算符而不是 apply 函数。

$ node plucking2.js
The oldest person is 81 years old.

Ramda split list

使用 splitEvery 函数,我们可以将列表拆分为指定长度的块。

chunks.js
import * as R from 'ramda';

let nums = [1, 2, 3, 4, 5, 6];

console.log(R.splitEvery(2, nums));
console.log(R.splitEvery(3, nums));

在此示例中,我们将数组拆分为 2 个和 3 个元素的块。

$ node chunks.js
[ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ]
[ [ 1, 2, 3 ], [ 4, 5, 6 ] ]

Ramda contains 函数

如果列表中包含指定的值,则 contains 函数返回 true。

containsfun.js
import * as R from 'ramda';

const users = [
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Andrew', age: 43 },
  { name: 'Peter', age: 81 },
  { name: 'Anna', age: 43 },
  { name: 'Albert', age: 76 },
  { name: 'Adam', age: 47 },
  { name: 'Robert', age: 72 }
];

let isJohn = R.contains('John', R.pluck('name', users));

if (isJohn) {

  console.log('There is John in the list');
}

在此示例中,我们检查指定的用户是否在列表中。

let isJohn = R.contains('John', R.pluck('name', users));

首先,我们使用 pluck 函数从 name 属性形成一个列表。然后我们用 contains 检查 'John' 是否在列表中。

$ node containsfun.js
There is John in the list

Ramda range 函数

range 函数返回一个数字列表,从起始值(包括)到结束值(不包括)。

rangefun.js
import * as R from 'ramda';

console.log(R.range(1, 10));

let vals = R.range(2, 12);

vals.forEach(x => console.log(x));

此示例显示了如何使用 range 函数。

console.log(R.range(1, 10));

在此行中,我们创建了一个 1..9 整数的列表。我们将它们打印到控制台。

let vals = R.range(2, 12);

vals.forEach(x => console.log(x));

这里我们生成一个 2..11 值的列表。我们使用 forEach 函数遍历列表。

$ node rangefun.js
[ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
2
3
4
5
6
7
8
9
10
11

Ramda sum 函数

sum 函数对列表的所有元素求和。

summation.js
import * as R from 'ramda';

let nums = [2, 4, 6, 8, 10];
console.log(R.sum(nums));

console.log(R.sum(R.range(1, 11)));

此示例使用 sum 函数对整数值求和。

let nums = [2, 4, 6, 8, 10];
console.log(R.sum(nums));

这里我们对 nums 数组的值求和。

console.log(R.sum(R.range(1, 11)));

在此行中,我们对由 range 函数生成的列表的值求和。

$ node summation.js
30
55

Ramda product 函数

product 函数将列表的所有元素相乘。

productfun.js
import * as R from 'ramda';

let nums = [2, 4, 6, 8, 10];

console.log(R.product(nums));

此示例计算整数列表的乘积。

$ node productfun.js
3840

Ramda sort、reverse 函数

sort 函数返回根据比较器函数排序的列表的副本。比较器函数一次接受两个值,如果第一个值较小则返回一个负数,如果较大则返回一个正数,如果它们相等则返回零。

reverse 函数返回一个新列表或字符串,其中元素或字符的顺序相反。

sort_reverse.js
import * as R from 'ramda';

let nums = [3, 1, 4, 2, 8, 5, 6];

console.log('sorting:')

// sort ascending
console.log(R.sort((x, y) =>  x - y , nums));

// sort descending
console.log(R.sort((x, y) =>  y - x , nums));

console.log('reversing:')

// reversing
console.log(R.reverse(nums));
console.log(R.reverse('forest'));

此示例按升序和降序对整数进行排序,并颠倒整数和字符串的顺序。

$ node sort_reverse.js
sorting:
[ 1, 2, 3, 4, 5, 6, 8 ]
[ 8, 6, 5, 4, 3, 2, 1 ]
reversing:
[ 6, 5, 8, 2, 4, 1, 3 ]
tserof

我们还可以使用内置的 R.ltR.gt 比较器。

sort_comp.js
import * as R from 'ramda';

let nums = [3, 1, 4, 2, 8, 5, 6];

console.log('sorting:')

// sort ascending
console.log(R.sort(R.comparator(R.lt), nums));

// sort descending
console.log(R.sort(R.comparator(R.gt), nums));

此示例按升序和降序对整数进行排序。

Ramda sortBy 函数

sortBy 函数根据提供的函数对列表进行排序。

sorting_objects.js
import * as R from 'ramda';

const users = [
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Andrew', age: 43 },
  { name: 'Peter', age: 81 },
  { name: 'Anna', age: 43 },
  { name: 'Albert', age: 76 },
  { name: 'Adam', age: 47 },
  { name: 'Robert', age: 72 }
];

console.log('Sorted by age:');

let sortedByAge = R.sortBy(R.prop('age'), users);
console.log(sortedByAge);

console.log('Sorted by name:');

let sortedByName = R.sortBy(R.prop('name'), users);
console.log(sortedByName);

在此示例中,我们按升序对用户列表按 agename 属性排序。

$ node sorting_objects.js
Sorted by age:
[ { name: 'John', age: 25 },
  { name: 'Andrew', age: 43 },
  { name: 'Anna', age: 43 },
  { name: 'Adam', age: 47 },
  { name: 'Lenny', age: 51 },
  { name: 'Robert', age: 72 },
  { name: 'Albert', age: 76 },
  { name: 'Peter', age: 81 } ]
Sorted by name:
[ { name: 'Adam', age: 47 },
  { name: 'Albert', age: 76 },
  { name: 'Andrew', age: 43 },
  { name: 'Anna', age: 43 },
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Peter', age: 81 },
  { name: 'Robert', age: 72 } ]

Ramda find、findLast 函数

find 函数返回与谓词匹配的列表的第一个元素,如果没有任何匹配项则返回 undefined。 findLast 函数返回与谓词匹配的列表的最后一个元素,如果没有任何元素匹配,则返回 undefined。

finding.js
import * as R from 'ramda';

const isPositive = x => x > 0;

let values = [-1, 0, -4, 5, 6, -1, 9, -2]

let val = R.find(isPositive, values);
console.log(val);

let val2 = R.findLast(isPositive, values);
console.log(val2);

在此示例中,我们找到第一个和最后一个正值。

const isPositive = x => x > 0;

isPositive 是一个谓词函数,对于大于零的值返回 true。

let val = R.find(isPositive, values);

使用 find,我们找到正数的第一次出现。

let val2 = R.findLast(isPositive, values);

使用 findLast,我们找到正数的最后一次出现。

$ node finding.js
5
9

第一个正值是 5,最后一个是 9。

在以下示例中,我们将 find 函数用于对象列表。

finding2.js
import * as R from 'ramda';

const users = [
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Andrew', age: 43 },
  { name: 'Peter', age: 81 },
  { name: 'Anna', age: 43 },
  { name: 'Albert', age: 76 },
  { name: 'Adam', age: 47 },
  { name: 'Robert', age: 72 },
  { name: 'Robert', age: 26 },
];

console.log(R.find(R.propEq('name', 'Robert'))(users));
console.log(R.find(R.propEq('age', 81))(users));

通过组合 findpropEq 函数,我们查找具有指定属性的用户。

console.log(R.find(R.propEq('name', 'Robert'))(users));

这里我们查找名为 Robert 的人。 有两个 Robert,返回第一个匹配项。

$ node finding2.js
{ name: 'Robert', age: 72 }
{ name: 'Peter', age: 81 }

Ramda map 函数

map 函数在每个容器的值上映射一个提供的函数。

mapping.js
import * as R from 'ramda';

nums = [2, 4, 5, 6, 7, 8, 9];

let res = R.map(x => x * 2, nums);
console.log(res);

const isEven = x => x % 2 === 0;
let res2 = R.map(isEven, nums);
console.log(res2);

let repeated = R.map(R.call, R.repeat(Math.random, 5));
console.log(repeated);

此示例演示了 map 的用法。

let res = R.map(x => x * 2, nums);

我们对整数列表映射一个匿名函数。生成一个新列表,其中每个值乘以 2。

const isEven = x => x % 2 === 0;
let res2 = R.map(isEven, nums);

这里我们对每个元素应用 isEven 函数。res2 是一个包含 true 和 false 值的列表。 如果我们只想选择偶数,那么我们将使用 filter 函数。

let repeated = R.map(R.call, R.repeat(Math.random, 5));

在第三种情况下,我们生成一个包含五个随机值的列表。

$ node mapping.js
[ 4, 8, 10, 12, 14, 16, 18 ]
[ true, true, false, true, false, true, false ]
[ 0.22019193556521865,
  0.415950206671615,
  0.8770997167119405,
  0.23393806619678315,
  0.8181008680173825 ]

Ramda filter 函数

filter 函数根据提供的谓词函数过滤可过滤(例如列表或纯对象)。(谓词是一个返回布尔值的函数)。

filtering.js
import * as R from 'ramda';

nums = [-3, -1, 0, 2, 3, 4, 5, 6, 7]

let res = R.filter(x => x > 0, nums);
console.log(res);

let res2 = R.filter(x => x < 0, nums);
console.log(res2);

const isEven = x => x % 2 === 0;

let filtered = R.filter(isEven, nums);
console.log(filtered);

在此示例中,我们有一个整数值列表。我们使用 filter 函数过滤掉正数、负数和偶数值。

let res = R.filter(x => x > 0, nums);

此行中的 filter 函数接受一个匿名函数,该函数对于所有大于零的值返回 true。然后将谓词应用于列表的每个元素。 这样我们形成一个仅包含正值的新列表。

$ node filtering.js
[ 2, 3, 4, 5, 6, 7 ]
[ -3, -1 ]
[ 0, 2, 4, 6 ]

在以下示例中,我们将 filter 函数应用于用户列表。

filtering2.js
import * as R from 'ramda';

// senior is a person who is 70+

const users = [
  { name: 'John', age: 25 },
  { name: 'Lenny', age: 51 },
  { name: 'Andrew', age: 43 },
  { name: 'Peter', age: 81 },
  { name: 'Anna', age: 43 },
  { name: 'Albert', age: 76 },
  { name: 'Adam', age: 47 },
  { name: 'Robert', age: 72 }
];

console.log(R.filter(user => user.age >= 70, users));

此示例过滤掉高级用户。我们将高级用户定义为 70 岁及以上的人。

$ node filtering2.js
[ { name: 'Peter', age: 81 },
  { name: 'Albert', age: 76 },
  { name: 'Robert', age: 72 } ]

我们有三个高级用户。

reject 函数

rejectfilter 的补充。 它排除了谓词返回 true 的可过滤元素。

rejecting.js
import * as R from 'ramda';

const users = [
    { name: 'John', city: 'London', born: '2001-04-01' },
    { name: 'Lenny', city: 'New York', born: '1997-12-11' },
    { name: 'Andrew', city: 'Boston', born: '1987-02-22' },
    { name: 'Peter', city: 'Prague', born: '1936-03-24' },
    { name: 'Anna', city: 'Bratislava', born: '1973-11-12' },
    { name: 'Albert', city: 'Bratislava', born: '1940-18-19' },
    { name: 'Adam', city: 'Trnava', born:'1983-12-01' },
    { name: 'Robert', city: 'Bratislava', born: '1935-05-15' },
    { name: 'Robert', city: 'Prague', born:'1998-03-14' }
  ];

let res = R.reject(R.propEq('city', 'Bratislava'))(users);
console.log(res);

let res2 = R.filter(R.propEq('city', 'Bratislava'))(users);
console.log(res2);

在此示例中,我们使用 reject 函数形成一个不包含 Bratislava 城市的新对象列表。 我们还使用 filter 函数形成一个包含 Bratislava 城市的新对象列表。

$ node rejecting.js
[ { name: 'John', city: 'London', born: '2001-04-01' },
  { name: 'Lenny', city: 'New York', born: '1997-12-11' },
  { name: 'Andrew', city: 'Boston', born: '1987-02-22' },
  { name: 'Peter', city: 'Prague', born: '1936-03-24' },
  { name: 'Adam', city: 'Trnava', born: '1983-12-01' },
  { name: 'Robert', city: 'Prague', born: '1998-03-14' } ]
[ { name: 'Anna', city: 'Bratislava', born: '1973-11-12' },
  { name: 'Albert', city: 'Bratislava', born: '1940-18-19' },
  { name: 'Robert', city: 'Bratislava', born: '1935-05-15' } ]

这是输出。 第一个列表包含不包含 Bratislava 城市属性的所有对象。 第二个仅包含具有 Bratislava 城市属性的对象。

partition 函数

partition 函数将可过滤内容分成两个单独的对象:一个满足谓词,一个不满足谓词。

partitionfun.js
import * as R from 'ramda';

let nums = [4, -5, 3, 2, -1, 7, -6, 8, 9];

let [ neg, pos ] = R.partition(e => e < 0, nums);

console.log(neg);
console.log(pos);

使用 partition 函数,我们将整数列表拆分为两个单独的列表:负数和正数。

$ node partitionfun.js
[ -5, -1, -6 ]
[ 4, 3, 2, 7, 8, 9 ]

第一个列表包含负值,第二个列表包含正值。

Ramda groupBy 函数

groupBy 函数将列表拆分为存储在对象中的子列表,基于对每个元素调用 String-returning 函数的结果,并根据返回的值对结果进行分组。

grouping.js
import * as R from 'ramda';

let students = [
  { name: 'Adam', score: 84 },
  { name: 'Eddy', score: 58 },
  { name: 'Peter', score: 69 },
  { name: 'Roman', score: 93 },
  { name: 'Jane', score: 56 },
  { name: 'Lucy', score: 76 },
  { name: 'Jack', score: 88 },
];

var groupByGrade = R.groupBy((student) => {

  let score = student.score;

  return score < 65 ? 'F' :
         score < 70 ? 'D' :
         score < 80 ? 'C' :
         score < 90 ? 'B' : 'A';
});

let grouped = groupByGrade(students);

console.log('Student(s) having A grade:');
console.log(grouped['A']);

console.log('Student(s) having B grade:');
console.log(grouped['B']);

console.log('Student(s) having C grade:');
console.log(grouped['D']);

console.log('Student(s) having D grade:');
console.log(grouped['D']);

console.log('Student(s) having F grade:');
console.log(grouped['F']);

在此示例中,我们按分数将学生分组到等级子列表中。

$ node grouping.js
Student(s) having A grade:
[ { name: 'Roman', score: 93 } ]
Student(s) having B grade:
[ { name: 'Adam', score: 84 }, { name: 'Jack', score: 88 } ]
Student(s) having C grade:
[ { name: 'Peter', score: 69 } ]
Student(s) having D grade:
[ { name: 'Peter', score: 69 } ]
Student(s) having F grade:
[ { name: 'Eddy', score: 58 }, { name: 'Jane', score: 56 } ]

Ramda reduce 函数

reduce 函数将列表值聚合为单个值。它将一个函数应用于累加器和列表中的每个元素(从左到右),以将其简化为单个值。

reducefun.js
import * as R from 'ramda';

let nums = [2, 3, 4, 5, 6, 7];

let sum = R.reduce((x, y) => x+y, 0, nums);
console.log(sum);

let product = R.reduce((x, y) => x*y, 1, nums);
console.log(product);

此示例使用 reduce 函数计算整数列表的总和和乘积。

let sum = R.reduce((x, y) => x+y, 0, nums);

在此行中,我们计算值的总和。第一个参数是应用于这些值的函数。第二个是累加器,它是起始值。第三个是包含值的列表。

let product = R.reduce((x, y) => x*y, 1, nums);

这里我们计算列表值的乘积。

$ node reducefun.js
27
5040

以下示例计算表达式:1*2 + 3*4 + 5*6

reduce_fun2.js
import * as R from 'ramda';

let nums = [1, 2, 3, 4, 5, 6];

let ret = R.reduce((acc, x) => acc + x[0] * x[1], 0, R.splitEvery(2, nums));
console.log(ret);

在此示例中,我们将列表拆分为对,并对这些对应用 reduce 操作。

$ node reduce_fun2.js
44

Ramda where 函数

where 函数允许对对象创建复杂的查询。

wherefun.js
import * as R from 'ramda';
const moment = require('moment');

const users = [
  { name: 'John', city: 'London', born: '2001-04-01' },
  { name: 'Lenny', city: 'New York', born: '1997-12-11' },
  { name: 'Andrew', city: 'Boston', born: '1987-02-22' },
  { name: 'Peter', city: 'Prague', born: '1936-03-24' },
  { name: 'Anna', city: 'Bratislava', born: '1973-11-18' },
  { name: 'Albert', city: 'Bratislava', born: '1940-12-11' },
  { name: 'Adam', city: 'Trnava', born: '1983-12-01' },
  { name: 'Robert', city: 'Bratislava', born: '1935-05-15' },
  { name: 'Robert', city: 'Prague', born: '1998-03-14' }
];

let res1 = R.filter(R.where({ city: R.equals('Bratislava') }))(users);
console.log(res1);

let res2 = R.filter(R.where({
  city: R.equals('Bratislava'),
  name: R.startsWith('A')
}))(users);

console.log(res2);

let res3 = R.filter(R.where({
  born: (dt) => getAge(dt) > 40}))(users);

console.log(res3);


function getAge(dt) {

    return moment.duration(moment() - moment(dt, 'YYYY-MM-DD', true)).years();
}

在此示例中,我们在用户列表上使用 where 创建查询。

let res1 = R.filter(R.where({ city: R.equals('Bratislava') }))(users);

这里我们找出所有住在 Bratislava 的用户。

let res2 = R.filter(R.where({
  city: R.equals('Bratislava'),
  name: R.startsWith('A')
}))(users);

在此代码中,我们找出住在 Bratislava 并且他们的名字以 'A' 开头的用户。

let res3 = R.filter(R.where({
  born: (dt) => getAge(dt) > 40}))(users);

最后,我们找出年龄大于 40 岁的用户。

function getAge(dt) {

    return moment.duration(moment() - moment(dt, 'YYYY-MM-DD', true)).years();
}

为了从提供的出生日期计算年龄,我们使用 moment 模块。

$ node where_fun.js
[ { name: 'Anna', city: 'Bratislava', born: '1973-11-18' },
  { name: 'Albert', city: 'Bratislava', born: '1940-12-11' },
  { name: 'Robert', city: 'Bratislava', born: '1935-05-15' } ]
[ { name: 'Anna', city: 'Bratislava', born: '1973-11-18' },
  { name: 'Albert', city: 'Bratislava', born: '1940-12-11' } ]
[ { name: 'Peter', city: 'Prague', born: '1936-03-24' },
  { name: 'Anna', city: 'Bratislava', born: '1973-11-18' },
  { name: 'Albert', city: 'Bratislava', born: '1940-12-11' },
  { name: 'Robert', city: 'Bratislava', born: '1935-05-15' } ]

来源

Ramda 文档

在本文中,我们使用了 Ramda 库。

作者

我叫 Jan Bodnar,是一位对编程充满热情的程序员,拥有丰富的编程经验。 我从 2007 年开始撰写编程文章。 迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

查看 所有 JavaScript 教程。