javascript - 数组中的 lodash 过滤器
问题描述
在 lodash 中是否有可能在对象中的数组中进行过滤?
我有一个对象,其中有一个数组。看起来像这样
{
"id": "1",
"name": "Test 1",
"tag": ["blue","red", "yellow"]
},
{
"id": "2",
"name": "Test 2",
"tag": ["red", "yellow"]
},
{
"id": "3",
"name": "Test 3",
"tag": ["green"]
}
我现在想做什么。如果标签是红色,他应该输出 id 为 1 和 2 的对象。标签 = Green 只输出 id 为 3 的对象。依此类推。
我现在尝试使用 lodash 过滤器来解决这个问题。
const filteredColors = _.filter(colors, function(c) {
return _.includes(['Test 1', 'Test 2'], c.name);
});
// returns Objects with 2 Entrys = Correct
我可以过滤正常值,但是如何在数组中找到值?
解决方案
我已经解决了:
let filter = _.filter(
colors,
_.flow(
_.property('tag'),
_.partial(_.intersection, ['red', 'green']),
_.size,
),
);
推荐阅读
- python - 在条件下获取随机元素
- php - 如何使用php返回ajax
- node.js - 如何使用 Angular-Cli 创建部署到 ZIP ./dist 文件夹的过程
- java - 没有硬编码值的外观和感觉
- node.js - AWS 弹性 beanstalk Node.js 应用程序未连接到 Mongodb 地图集
- python - 如何在“add_with_ids”训练索引上返回“faiss”唯一向量 id?
- python - 有没有办法一次在多个文件上使用 COPY?
- angular - 在应用程序中使用 config.json 文件的参数
- java - Android recyclerview在调用notifyItemChanged时不更新持有者,有效载荷没有价值
- dependency-injection - 具有多个终结点和依赖项注入的 Service Fabric