Lodash:当我嵌套Object时,如何使用filter?
考虑这个例子。 我正在使用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 }, 当我做
 _.filter(summary.data, {'debit': true}) 
我把所有的东西都拿回来了。
我想要的是?
 我想要所有的对象category.parent == 'Food' ,我该怎么做? 
我试过了
 _.filter(summary.data, {'category.parent': 'Food'}) 
得到了
 [] 
	
 _.filter(summary.data, function(item){ return item.category.parent === 'Food'; }); 
lodash允许嵌套的对象定义:
 _.filter(summary.data, {category: {parent: 'Food'}}); 
从v3.7.0开始,lodash还允许在string中指定对象键:
 _.filter(summary.data, ['category.parent', 'Food']); 
JSFiddle中的示例代码: https ://jsfiddle.net/6qLze9ub/
lodash也支持与数组嵌套; 如果要过滤其中一个数组项(例如,如果category是一个数组):
 _.filter(summary.data, {category: [{parent: 'Food'}] }); 
如果你真的需要一些自定义比较,那么传递一个函数的时候:
 _.filter(summary.data, function(item) { return _.includes(otherArray, item.category.parent); }); 
 从v3.7.0开始,你可以这样做: 
 _.filter(summary.data, 'category.parent', 'Food') 
 _.where(summary.data, {category: {parent: 'Food'}}); 
也应该做的伎俩
在lodash 4.x中,你需要做的是:
 _.filter(summary.data, ['category.parent', 'Food']) 
(注意数组围绕第二个参数)。
这相当于调用:
 _.filter(summary.data, _.matchesProperty('category.parent', 'Food')) 
  这里是_.matchesProperty的文档 : 
 // The `_.matchesProperty` iteratee shorthand. _.filter(users, ['active', false]); // => objects for ['fred']