如何使用搜索栏在ionic 4搜索栏中过滤嵌套数组对象
下面的ionic版本4.7.1我附加了Filter函数,但无法使用此函数来过滤嵌套数组中的对象
this.filterData = this.allData.filter(data => {if (data.letter && this.searchTerm) {
for (var i = 0; i < data.child_contacts.length; i++) {
if (
data.child_contacts[i].full_name
.toLowerCase()
.indexOf(this.searchTerm.toLowerCase()) > -1
) {
return (
data.child_contacts[i].full_name
.toLowerCase()
.indexOf(this.searchTerm.toLowerCase()) > -1
);
}
}
return false;}});
这是需要过滤的数据
this.allData=[ {
"letter":"A",
"child_contact":[
{"full_name":"First"},
{"full_name":"second"},
{"full_name":"third"}
]
},
{
"letter":"B",
"child_contact":[
{"full_name":"First second"},
{"full_name":"Third"}
]
},
{
"letter":"C",
"child_contact":[
{"full_name":"Third"}
]
} ]
如果我搜索“第一个”,我的预期输出将是
[ {
"letter":"A",
"child_contact":[
{
"full_name":"First"
},
]
},
{
"letter":"B",
"child_contact":[
{
"full_name":"First"
},
]
}]
答案 0 :(得分:0)
我不确定这是否是完整的解决方案,但是我可以看到的一个问题是您在过滤器中使用child_contacts
和s
for (var i = 0; i < data.child_contacts.length; i++) {
但数据为child_contact
:
this.allData=[ {
"letter":"A",
"child_contact":[
{"full_name":"First"},
{"full_name":"second"},
{"full_name":"third"}
]
},