Consider this example. I am using Lodash
考虑一下这个例子。我用Lodash
'data': [
{
'category': {
'uri': '/categories/0b092e7c-4d2c-4eba-8c4e-80937c9e483d',
'parent': 'Food',
'name': 'Costco'
},
'amount': '15.0',
'debit': true
},
{
'category': {
'uri': '/categories/d6c10cd2-e285-4829-ad8d-c1dc1fdeea2e',
'parent': 'Food',
'name': 'India Bazaar'
},
'amount': '10.0',
'debit': true
},
{
'category': {
'uri': '/categories/d6c10cd2-e285-4829-ad8d-c1dc1fdeea2e',
'parent': 'Food',
'name': 'Sprouts'
},
'amount': '11.1',
'debit': true
},
When I do
当我做
_.filter(summary.data, {'debit': true})
I get all the objects back.
我把所有的东西都拿回来。
what I want?
我想要的吗?
I want all the objects where category.parent == 'Food'
, how can I do that?
我想要所有类别的对象。父母=“食物”,我怎么做呢?
I tried
我试着
_.filter(summary.data, {'category.parent': 'Food'})
and got
并得到了
[]
5 个解决方案
#1
37
_.filter(summary.data, function(item){
return item.category.parent === 'Food';
});
#2
121
lodash allows nested object definitions:
lodash允许嵌套对象定义:
_.filter(summary.data, {category: {parent: 'Food'}});
As of v3.7.0, lodash also allows specifying object keys in strings:
在v3.7.0中,lodash还允许在字符串中指定对象键:
_.filter(summary.data, ['category.parent', 'Food']);
Example code in JSFiddle: https://jsfiddle.net/6qLze9ub/
js小提琴中的示例代码:https://jsfiddle.net/6qLze9ub/
lodash also supports nesting with arrays; if you want to filter on one of the array items (for example, if category is an array):
lodash还支持数组嵌套;如果您想要筛选其中一个数组项(例如,如果类别是数组):
_.filter(summary.data, {category: [{parent: 'Food'}] });
If you really need some custom comparison, that's when to pass a function:
如果你真的需要一些自定义比较,那就是传递函数的时候了:
_.filter(summary.data, function(item) {
return _.includes(otherArray, item.category.parent);
});
#3
12
beginning from v3.7.0
you can do it in this way:
从v3.7.0开始,您可以这样做:
_.filter(summary.data, 'category.parent', 'Food')
#4
3
_.where(summary.data, {category: {parent: 'Food'}});
Should do the trick too
你也应该这样做吗?
#5
2
In lodash 4.x, you need to do:
lodash 4。x,你需要做:
_.filter(summary.data, ['category.parent', 'Food'])
(note the array wrapping around the second argument).
(注意围绕第二个参数的数组)。
This is equivalent to calling:
这相当于打电话:
_.filter(summary.data, _.matchesProperty('category.parent', 'Food'))
Here are the docs for _.matchesProperty
:
这里是_.matchesProperty的文档:
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']
#1
37
_.filter(summary.data, function(item){
return item.category.parent === 'Food';
});
#2
121
lodash allows nested object definitions:
lodash允许嵌套对象定义:
_.filter(summary.data, {category: {parent: 'Food'}});
As of v3.7.0, lodash also allows specifying object keys in strings:
在v3.7.0中,lodash还允许在字符串中指定对象键:
_.filter(summary.data, ['category.parent', 'Food']);
Example code in JSFiddle: https://jsfiddle.net/6qLze9ub/
js小提琴中的示例代码:https://jsfiddle.net/6qLze9ub/
lodash also supports nesting with arrays; if you want to filter on one of the array items (for example, if category is an array):
lodash还支持数组嵌套;如果您想要筛选其中一个数组项(例如,如果类别是数组):
_.filter(summary.data, {category: [{parent: 'Food'}] });
If you really need some custom comparison, that's when to pass a function:
如果你真的需要一些自定义比较,那就是传递函数的时候了:
_.filter(summary.data, function(item) {
return _.includes(otherArray, item.category.parent);
});
#3
12
beginning from v3.7.0
you can do it in this way:
从v3.7.0开始,您可以这样做:
_.filter(summary.data, 'category.parent', 'Food')
#4
3
_.where(summary.data, {category: {parent: 'Food'}});
Should do the trick too
你也应该这样做吗?
#5
2
In lodash 4.x, you need to do:
lodash 4。x,你需要做:
_.filter(summary.data, ['category.parent', 'Food'])
(note the array wrapping around the second argument).
(注意围绕第二个参数的数组)。
This is equivalent to calling:
这相当于打电话:
_.filter(summary.data, _.matchesProperty('category.parent', 'Food'))
Here are the docs for _.matchesProperty
:
这里是_.matchesProperty的文档:
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']