Javascript lodsh过滤器不适用于对象数组
我有一个对象数组,看起来像Javascript lodsh过滤器不适用于对象数组,javascript,lodash,Javascript,Lodash,我有一个对象数组,看起来像 const test = [{"Active": {"Id":'1', 'Name': 'Peter'}, 'Collect' : {'Id':'2', 'Name': 'John'}},{"Active": {"Id":'1', 'Name': 'Peter'}, 'Collect' : {'Id':'2', 'Name': 'tru'}},{"Active": {&
const test = [{"Active": {"Id":'1', 'Name': 'Peter'}, 'Collect' : {'Id':'2', 'Name': 'John'}},{"Active": {"Id":'1', 'Name': 'Peter'}, 'Collect' : {'Id':'2', 'Name': 'tru'}},{"Active": {"Id":'1', 'Name': 'joe'}, 'Collect' : {'Id':'2', 'Name': 'mark'}}]
在这里,我试图使用的过滤器在哪里
tobefilter = "Peter"
现在,我使用下面的方法
const filterdata = _.filter(test => test.Active.Name === tobefilter)
这将返回
空数组
。有人能帮我吗?您缺少第一个参数,即要筛选的数组。这是正确的方法:
const filterdata=\u0.filter(测试,(x)=>x.Active.Name==tobefilter)
像这样试试
您没有传递数组以获取筛选器
const filterdata = _.filter(test, (t)=> t.Active.Name === tobefilter)
在本例中,缺少第一个参数—要筛选的数组
const filterdata = _.filter(test, test => test.Active.Name === tobefilter)
但是您可以这样做,只要记住首先构造过滤的源数据()
const test=[{“Active”:{“Id”:'1',Name':'Peter'},{“Collect':{“Id”:'2',Name':'joe'},{“Active”:{“Id”:'1',Name':'Peter'},'Collect':{Id':'2',Name':'tru'},{“Active Id”:'1',Name':'joe'},{“Collect':{“Id':'2',Name':'mark
const tobefilter=“彼得”
常量filterdata=(测试).filter(测试=>test.Active.Name===tobefilter)
console.log(filterdata)
这个案子你真的需要洛达斯吗?我的意思是,这是一艘没有它的单班轮
const test=[{“Active”:{“Id”:'1',Name':'Peter'},{“Collect':{“Id”:'2',Name':'joe'},{“Active”:{“Id”:'1',Name':'Peter'},'Collect':{Id':'2',Name':'tru'},{“Active Id”:'1',Name':'joe'},{“Collect':{“Id':'2',Name':'mark
常量filterdata=test.filter(test=>test.Active.Name===“Peter”)
console.log(filterdata)代码>你不需要洛达斯。只要用测试替换\uu
,它就会做你想要的事情。
const filterdata = _(test).filter(test => test.Active.Name === tobefilter)