在 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 的对象。标签 = 绿色,只有 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,
),
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句