🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Ramda 教程 > 原文: [http://zetcode.com/javascript/ramda/](http://zetcode.com/javascript/ramda/) Ramda 教程展示了如何使用 Ramda 库,该库为 JavaScript 中的高级函数编程提供了工具。 在本教程中,我们可以互换使用术语列表和数组。 ## Ramda Ramda 是 JavaScript 程序员的实用函数库。 该库专注于不变性和无副作用的函数。 Ramda 函数也会自动进行更新,从而只需不提供最终参数就可以从旧函数中构建新函数。 在本教程中,我们在 Node 应用中使用 Ramda。 ## 安装 Ramda 首先,我们安装 Ramda。 ```js $ nodejs -v v9.11.2 ``` 我们使用 Node 版本 9.11.2。 ```js $ npm init ``` 我们启动一个新的 Node 应用。 ```js $ npm i ramda ``` 我们使用`npm i ramda`命令安装 Ramda。 ```js const R = require('ramda'); ``` 按照惯例,该库使用 R 字母。 ## Ramda `add()`,`subtract()`函数 `add()`函数将两个值相加,`subtract()`函数将两个值相减。 `add_sub.js` ```js const R = require('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); ``` 该示例同时使用`add()`和`subtract()`函数。 ```js let res = R.add(R.add(2, 5), R.subtract(2, 10)); ``` 在这里,我们结合了这些函数。 ```js $ node add_sub.js 7 -3 -1 ``` 这是输出。 ## Ramda `flip()`函数 `flip()`函数从提供的函数返回一个新函数,该函数的参数相反。 `flipfun.js` ```js const R = require('ramda'); let val = R.subtract(2, 10); console.log(val); let val2 = R.flip(R.subtract)(2, 10); console.log(val2); ``` 该示例使用`flip()`反转`subtract()`函数的参数。 ```js $ node flipfun.js -8 8 ``` 这是输出。 ## Ramda `call()`函数 `call()`函数在用逗号分隔的参数上调用提供的函数。 `calling.js` ```js const R = require('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()`函数。 ```js let res = R.call(R.add, 1, 2); ``` 我们调用`add()`函数将两个整数相加。 ```js console.log(R.call(R.repeat, 'x')(5)); ``` 我们调用`repeat()`函数来生成五个`"x"`字母的列表。 ```js R.call(console.log, [1, 2, 3]); ``` 最后,我们使用`call()`函数输出列表。 ```js $ node calling.js 3 [ 'x', 'x', 'x', 'x', 'x' ] [ 1, 2, 3 ] ``` 这是输出。 ## Ramda `apply()`函数 `apply()`函数在参数列表上调用提供的函数。 `applyfun.js` ```js const R = require('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()`函数来计算最小值和最大值。 ```js let res = R.apply(Math.min, nums); ``` 我们在`nums`列表上调用`Math.min`函数。 我们从这些值中获得最小值。 ```js $ node applyfun.js 1 8 ``` 我们得到最小和最大。 ## Ramda 自动柯里 柯里化是将需要多个参数的函数转换为另一个函数的过程,当提供较少的参数时,该函数将返回一个等待其余参数的新函数。 `currying.js` ```js const R = require('ramda'); let addOneToAll = R.map(R.add(1)); let res = addOneToAll([1,2,3]); console.log(res); ``` 在示例中,我们创建了一个`addOneToAll()`函数,该函数将列表中的每个元素加 1。 ```js $ node currying.js [ 2, 3, 4 ] ``` 这是输出。 ## Ramda `head()`,`tail()`,`init()`,`last()`函数 `head()`返回给定列表或字符串的第一个元素。 `tail()`返回给定列表或字符串的除第一个元素外的所有元素。 `init()`返回给定列表或字符串的最后一个元素以外的所有元素。 `last()`返回给定列表或字符串的最后一个元素。 `head_tail.js` ```js const R = require('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)); ``` 该示例在值数组上使用`head()`,`tail()`,`init()`和`last()`函数。 ```js $ node head_tail.js 2 [ 4, 6, 8, 10 ] [ 2, 4, 6, 8 ] 10 ``` 这是输出。 ## Ramda `length()`函数 `length()`函数返回列表中的元素数。 `lengthfun.js` ```js const R = require('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); ``` 在示例中,我们计算列表中的元素数和列表中的唯一元素数。 ```js $ node lengthfn.js 12 7 ``` 列表中有十二个元素,列表中有七个唯一元素。 ## Ramda `prop()`函数 `prop()`函数返回对象的指定属性(如果存在)。 `propfun.js` ```js const R = require('ramda'); console.log(R.prop('name', { name: 'John', age: 25 })); console.log(R.prop('age', { name: 'John', age: 25 })); ``` 使用`prop()`函数,我们可以获得`name`和`age`属性的值。 ```js $ node propfun.js John 25 ``` 这是输出。 ## Ramda `pluck()`函数 `pluck()`函数通过从提供的列表中的所有对象上拔出指定的属性来返回新列表。 `plucking.js` ```js const R = require('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()`函数,我们获得`name`和`age`属性,并形成两个新列表。 ```js $ node plucking.js [ 25, 51, 43, 81, 43, 76, 47, 72 ] [ 'John', 'Lenny', 'Andrew', 'Peter', 'Anna', 'Albert', 'Adam', 'Robert' ] ``` 在下面的示例中,我们将使用形成的列表。 `plucking2.js` ```js const R = require('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.`); ``` 在示例中,我们找出一个人的最大年龄。 ```js let maxAge = R.apply(Math.max, R.pluck('age', users)); ``` 通过在年龄列表上调用`Math.max()`函数,我们可以获得最老的年龄。 ```js // let maxAge = Math.max(... R.pluck('age', users)); ``` 另一种带注释的解决方案使用扩展运算符代替`apply()`函数。 ```js $ node plucking2.js The oldest person is 81 years old. ``` 这是输出。 ## Ramda 拆分列表 使用`splitEvery()`函数,我们可以将列表分成指定长度的块。 `chunks.js` ```js const R = require('ramda'); let nums = [1, 2, 3, 4, 5, 6]; console.log(R.splitEvery(2, nums)); console.log(R.splitEvery(3, nums)); ``` 在示例中,我们将数组分为 2 个元素和 3 个元素的块。 ```js $ node chunks.js [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ] [ [ 1, 2, 3 ], [ 4, 5, 6 ] ] ``` 这是输出。 ## Ramda `contains()`函数 如果指定的值在列表中,则`contains()`函数返回`true`。 `containsfun.js` ```js const R = require('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'); } ``` 在示例中,我们检查指定的用户是否在列表中。 ```js let isJohn = R.contains('John', R.pluck('name', users)); ``` 首先,我们使用`pluck()`函数从`name`属性中形成一个列表。 然后我们用`contains()`检查'John'是否在列表中。 ```js $ node containsfun.js There is John in the list ``` 这是输出。 ## Ramda `range()`函数 `range()`函数返回从起始值(包含)到结束值(不含)的数字列表。 `rangefun.js` ```js const R = require('ramda'); console.log(R.range(1, 10)); let vals = R.range(2, 12); vals.forEach(x => console.log(x)); ``` 该示例显示了如何使用`range()`函数。 ```js console.log(R.range(1, 10)); ``` 在这一行中,我们创建一个`1..9`整数列表。 我们将它们打印到控制台。 ```js let vals = R.range(2, 12); vals.forEach(x => console.log(x)); ``` 在这里,我们生成一个`2..11`值的列表。 我们使用`forEach()`函数浏览列表。 ```js $ 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` ```js const R = require('ramda'); let nums = [2, 4, 6, 8, 10]; console.log(R.sum(nums)); console.log(R.sum(R.range(1, 11))); ``` 该示例使用`sum()`函数对整数值求和。 ```js let nums = [2, 4, 6, 8, 10]; console.log(R.sum(nums)); ``` 在这里,我们对`nums`数组的值求和。 ```js console.log(R.sum(R.range(1, 11))); ``` 在这一行中,我们对`range()`函数生成的列表的值求和。 ```js $ node summation.js 30 55 ``` 这是输出。 ## Ramda `product()`函数 `product()`函数将列表的所有元素相乘。 `productfun.js` ```js const R = require('ramda'); let nums = [2, 4, 6, 8, 10]; console.log(R.product(nums)); ``` 该示例计算整数列表的乘积。 ```js $ node productfun.js 3840 ``` 这是输出。 ## Ramda `sort()`,`reverse()`函数 `sort()`函数返回列表的副本,该列表根据比较器函数排序。 比较器函数一次接受两个值,如果第一个值较小,则返回一个负数;如果较大,则返回一个正数;如果相等,则返回零。 `reverse()`函数以相反的顺序返回带有元素或字符的新列表或字符串。 `sort_reverse.js` ```js const R = require('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')); ``` 该示例按升序和降序对整数进行排序,并反转整数和字符串。 ```js $ 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.lt`和`R.gt`比较器。 `sort_comp.js` ```js const R = require('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` ```js const R = require('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); ``` 在示例中,我们按`age`和`name`属性以升序对用户列表进行排序。 ```js $ 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` ```js const R = require('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); ``` 在示例中,我们找到第一个和最后一个正值。 ```js const isPositive = x => x > 0; ``` `isPositive()`是一个谓词函数,对于大于零的值返回`true`。 ```js let val = R.find(isPositive, values); ``` 使用`find()`,我们发现第一个出现的正数。 ```js let val2 = R.findLast(isPositive, values); ``` 使用`findLast()`,我们找到最后一个正数。 ```js $ node finding.js 5 9 ``` 第一个正值为 5,最后为 9。 在下面的示例中,我们在对象列表上使用`find()`函数。 `finding2.js` ```js const R = require('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)); ``` 通过`find()`和`propEq()`函数的组合,我们寻找具有指定属性的用户。 ```js console.log(R.find(R.propEq('name', 'Robert'))(users)); ``` 在这里,我们寻找一个名叫罗伯特的人。 有两个罗伯茨(Roberts),并且第一场比赛被返回。 ```js $ node finding2.js { name: 'Robert', age: 72 } { name: 'Peter', age: 81 } ``` 这是输出。 ## Ramda `map()`函数 `map()`函数将提供的函数映射到每个容器的值。 `mapping.js` ```js const R = require('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()`的用法。 ```js let res = R.map(x => x * 2, nums); ``` 我们将匿名函数映射到整数列表上。 生成一个新列表,其中每个值都乘以 2。 ```js const isEven = x => x % 2 === 0; let res2 = R.map(isEven, nums); ``` 在这里,我们在每个元素上应用`isEven()`函数。 `res2`是正确和错误值的列表。 如果我们只想选择事件号,则可以使用`filter()`函数。 ```js let repeated = R.map(R.call, R.repeat(Math.random, 5)); ``` 在第三种情况下,我们生成五个随机值的列表。 ```js $ 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` ```js const R = require('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()`函数过滤出正,负和偶数值。 ```js let res = R.filter(x => x > 0, nums); ``` 此行中的`filter()`函数采用匿名函数,该函数对于所有大于零的值都返回`true`。 然后将谓词应用于列表的每个元素。 这样,我们形成一个仅包含正值的新列表。 ```js $ node filtering.js [ 2, 3, 4, 5, 6, 7 ] [ -3, -1 ] [ 0, 2, 4, 6 ] ``` 这是输出。 在下面的示例中,我们将`filter()`函数应用于用户列表。 `filtering2.js` ```js const R = require('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 岁以上的人。 ```js $ node filtering2.js [ { name: 'Peter', age: 81 }, { name: 'Albert', age: 76 }, { name: 'Robert', age: 72 } ] ``` 我们有三个高级用户。 ## `reject()`函数 `reject()`是`filter()`的补充。 它排除谓词为其返回`true`的可过滤元素。 `rejecting.js` ```js const R = require('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()`函数形成不包含布拉迪斯拉发城市的对象的新列表。 我们还使用`filter()`函数形成包含布拉迪斯拉发城市的对象的新列表。 ```js $ 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' } ] ``` 这是输出。 第一个列表包含不包含布拉迪斯拉发城市属性的所有对象。第二个列表仅包含具有布拉迪斯拉发城市属性的对象。 ## `partition()`函数 `partition()`函数将`filter`分为两个独立的对象:一个满足谓词,另一个不满足。 `partitionfun.js` ```js const R = require('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()`函数,我们将整数列表分为两个单独的列表:负数和正数。 ```js $ node partitionfun.js [ -5, -1, -6 ] [ 4, 3, 2, 7, 8, 9 ] ``` 第一个列表包含负值,第二个列表包含正值。 ## Ramda `groupBy`函数 `groupBy()`函数基于在每个元素上调用`String`返回函数并根据返回的值对结果进行分组的结果,将列表分为存储在对象中的子列表。 `grouping.js` ```js const R = require('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']); ``` 在此示例中,我们将学生按分数分组到成绩子列表中。 ```js $ 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` ```js const R = require('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()`函数来计算整数列表的总和。 ```js let sum = R.reduce((x, y) => x+y, 0, nums); ``` 在这一行中,我们计算值的总和。 第一个参数是应用于值的函数。 第二个是累加器,它是起始值。 第三是包含值的列表。 ```js let product = R.reduce((x, y) => x*y, 1, nums); ``` 在这里,我们计算列表值的乘积。 ```js $ node reducefun.js 27 5040 ``` 这是输出。 下面的示例计算表达式:`1*2 + 3*4 + 5*6`。 `reduce_fun2.js` ```js const R = require('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); ``` 在示例中,我们将列表分成几对,并对这些对应用归约操作。 ```js $ node reduce_fun2.js 44 ``` 这是输出。 ## `where()`函数 `where()`函数允许在对象上创建复杂的查询。 `wherefun.js` ```js const R = require('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()`创建查询。 ```js let res1 = R.filter(R.where({ city: R.equals('Bratislava') }))(users); ``` 在这里,我们找出居住在布拉迪斯拉发的所有用户。 ```js let res2 = R.filter(R.where({ city: R.equals('Bratislava'), name: R.startsWith('A') }))(users); ``` 在此代码中,我们找到了居住在布拉迪斯拉发的用户,其名称以`"A"`开头。 ```js let res3 = R.filter(R.where({ born: (dt) => getAge(dt) > 40}))(users); ``` 最后,我们找出 40 岁以上的用户。 ```js function getAge(dt) { return moment.duration(moment() - moment(dt, 'YYYY-MM-DD', true)).years(); } ``` 要从提供的出生日期算起年龄,我们使用矩模块。 ```js $ 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`库。 您可能也会对以下相关教程感兴趣: [Lodash 教程](/javascript/lodash/), [Collect.js 教程](/javascript/collectjs/), [JavaScript 数组](/javascript/arrays/), [Moment.js 教程](/javascript/momentjs/) 。