You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lodash overSome仅生效首个断言,如何同时匹配多个目标?

问题原因与解决方法

你的代码出错的核心原因是**overSome的传参方式错误**:Ramda的overSome函数要求接收一个谓词函数数组作为入参,而不是把多个函数直接拆成独立参数传入。

你当前的写法里,overSome(equals('zip'), equals('county'))只会把第一个函数equals('zip')当作有效参数,第二个equals('county')会被直接忽略,导致过滤逻辑实际上只匹配'zip'这一个值。

修正后的代码

const arr = ['county', 'zip', 'town'];
// 将两个判断函数放入数组,传给overSome
const func = filter(overSome([equals('zip'), equals('county')]));
const ans = func(arr);
console.log(ans);
// Output: ['county', 'zip']

更简洁的替代写法

如果只是简单匹配数组中的指定元素,用includes会更直观:

const arr = ['county', 'zip', 'town'];
const func = filter(includes(['zip', 'county']));
const ans = func(arr);
console.log(ans);
// Output: ['county', 'zip']

内容的提问来源于stack exchange,提问作者random

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 06:35:29