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

如何在Pydash中实现Lodash的find(数组,对象)匹配功能?

在Pydash中实现Lodash风格的find对象匹配

你可以用Pydash的_.matches()函数实现和Lodash里find(ARRAY_OF_OBJECTS, OBJECT)完全一致的功能,它会生成一个断言函数,自动检查目标对象是否包含匹配对象的所有键值对,完美适配动态生成的匹配条件。

举个实际示例:

import pydash as _

# 示例对象数组
items = [
    {"active": True, "dimension": "target", "status": "ok"},
    {"active": False, "dimension": "source", "status": "pending"},
    {"active": True, "dimension": "source", "status": "ok"}
]

# 动态生成的匹配条件对象
match_obj = {"active": True, "dimension": "target"}

# 使用matches生成匹配器,传入find方法执行查找
result = _.find(items, _.matches(match_obj))
print(result)
# 输出: {'active': True, 'dimension': 'target', 'status': 'ok'}

如果匹配对象的字段在数组元素中不存在,该元素会自动被排除。比如匹配对象是{"non_existent": "value"}时,find会返回None,因为没有元素包含这个键值对。

另外,matches默认采用宽松匹配逻辑:它只验证匹配对象里的所有键值对是否在目标对象中存在且相等,不会在意目标对象是否有额外的字段。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 19:22:06