我正在尝试过滤传递两个参数的对象数组,但是它不起作用...我的数组包含一个具有我传递参数值但结果为空的对象
<ListView Grid.Row="1"
ItemsSource="{Binding ContactsGrouped}"
IsGroupingEnabled="true"
IsPullToRefreshEnabled="True"
RefreshCommand="{Binding RefreshCommand}"
IsRefreshing="{Binding IsRefreshing}"
GroupDisplayBinding="{Binding Key}"
GroupShortNameBinding="{Binding Key}"
BackgroundColor="Transparent"
SelectionMode="Single"
HasUnevenRows="true"
SeparatorColor="#cccccc"
VerticalOptions="StartAndExpand">
<ListView.ItemTemplate>
<cr:MyItemTemplate>
</ListView.ItemTemplate>
</ListView>
我也尝试使用“过滤器”,但存在相同的问题...
const item = this.lista.find(i => i.number === rule.number && i.name === rule.name);
这是我的完整数组:
const item = this.lista.filter(i => i.number === rule.number).filter(i =>
i.name === rule.name);
这是规则对象:
this.lista= [
{number: 0, name: 'jose'},
{number: 2, name: 'maria'},
{number: 3, name: 'martha'},
{number: 4, name: 'ana'},
{number: 4, name: 'petter'},
];
有最好的方法吗?
答案 0 :(得分:0)
我不确定您的代码中出了什么问题,但是我已经复制了您的代码的一个简单示例,它可以按预期工作。
const rule = {};
rule.number = 4;
rule.name = 'ana';
const lista = [{
number: 0,
name: 'jose'
},
{
number: 2,
name: 'maria'
},
{
number: 3,
name: 'martha'
},
{
number: 4,
name: 'ana'
},
{
number: 4,
name: 'petter'
},
];
const item = lista.find(i => i.number === rule.number && i.name === rule.name);
console.log(item);