我有一个像这样的JSON:
filteredArray = [
{
"Key":"Brochure",
"Value":[{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"Accounting",
"Reprint":"Request",
"Contact":"Mike Astrup",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
},
{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 1",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
}]
},
{
"Key":"Handout",
"Value":[{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 2",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
},
{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 3",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
}]
}
]
我必须以Angular 2中的Industry为基础过滤数据。
我在Angular 2中的管道中使用此查询,但数据未被过滤。
filteredArray.filter(
item => item.Value.filter(
innerItem => innerItem.Industry.match(industry)))
答案 0 :(得分:0)
需要从两个过滤器返回数据:
var filteredArray = [
{
"Key":"Brochure",
"Value":[{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"Accounting",
"Reprint":"Request",
"Contact":"Mike Astrup",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
},
{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 1",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
}]
},
{
"Key":"Handout",
"Value":[{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 2",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
},
{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 3",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
}]
}
]
var test = filteredArray.filter((item) => {
return item.Value.filter((Initem) => {
return (Initem.Industry == "Accounting") //Filter for Accounting industry
})
})
console.log(test);
答案 1 :(得分:0)
利用Array.filter
和Array.some
let s = this.data.filter((d) => {
d.Value = d.Value.filter(d1 => {
if (d1.Industry === "Accounting") { // here put your condition
return d1;
}
});
if (d.Value.length > 0) {
return d;
}
});
var data = [
{
"Key":"Brochure",
"Value":[{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"Accounting",
"Reprint":"Request",
"Contact":"Mike Astrup",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
},
{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 1",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
}]
},
{
"Key":"Handout",
"Value":[{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 2",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
},
{
"Title":"Accounting Services",
"service":"Accounting",
"Location":"",
"Industry":"",
"Reprint":"Request",
"Contact":"Mike Astrup 3",
"Updated":"04/15/2017",
"Owner":"EB",
"Status":"Approved",
"online":"true",
"Type":"Material",
"Url":".common/service"
}]
}
];
var s = data.filter((d) => {
d.Value = d.Value.filter(d1 => {
if(d1.Industry === "Accounting") {
return d1;
}
});
if(d.Value.length > 0){
return d;
}
});
console.log(s);