如何使用ionic3过滤数组中的嵌套数据



我正在尝试根据用户名过滤数组并显示它,但是当我尝试过滤时,它不显示任何数据。 用户结构是这样的

buyer: [{
address: ""
contactNo: ""
email: "test@gmail.com"
name: "user"}]
cutomerContact: "3058989778"
orderDate: "2019-07-31T12:10:11.818Z"
orderType: "general"

我过滤数组的代码是

this.userOrders = this.filteredOrders.filter(x => x.buyer = x.buyer.name == this.userName)

我不知道如何过滤嵌套数据,任何帮助将不胜感激

用户filter过滤主数组,some function检查条件。

some(( 方法为数组中存在的每个元素执行一次函数:

  • 如果它找到函数返回 true 值的数组元素, some(( 返回 true(并且不检查剩余值(
  • 否则返回假

name = 'user';

filterData = [
{
"buyer": [
{
"address": "",
"contactNo": "",
"email": "user@gmail.com",
"name": "user"
}
],
"cutomerContact": "3058989778",
"orderDate": "2019-07-31T12:10:11.818Z",
"orderType": "general"
},
{
"buyer": [
{
"address": "",
"contactNo": "",
"email": "test@gmail.com",
"name": "test"
}
],
"cutomerContact": "3058989778",
"orderDate": "2019-07-31T12:10:11.818Z",
"orderType": "general"
}
]

let result = this.filterData.filter( (x) => {
return x.buyer.some(y => y.name == name)
})

console.log(result)

尝试以下操作

演示

let testArray =  [
{
"name": "SO",
"buyer": [
{
"address": "",
"contactNo": "",
"email": "test@gmail.com",
"name": "user"
}
],
"cutomerContact": "3058989778",
"orderDate": "2019-07-31T12:10:11.818Z",
"orderType": "general"
}
];
let filteredArray = testArray.filter((element) => element.buyer.some((subElement) => subElement.name == 'user')); 

console.log(filteredArray);

相关内容

  • 没有找到相关文章

最新更新