2017-07-26 76 views
-1

我有一个JSON像这样:如何在角度2中过滤对象内的对象?

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" 
     }] 
    } 
] 

我必须要过滤的工业基地中的数据角2

我在角2使用此查询管道,但该数据不得到过滤。

filteredArray.filter(
    item => item.Value.filter(
     innerItem => innerItem.Industry.match(industry))) 
+0

你是什么意思的行业过滤器?你需要按特定行业类型进行分组吗? –

+0

你想实现什么?从代码发布所需的输出 –

+0

我必须以相同格式过滤此json数据,但应根据行业过滤,即如果行业与Accounting相匹配。 –

回答

0

利用Array.filterArray.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) => { 
 
\t d.Value = d.Value.filter(d1 => { 
 
    if(d1.Industry === "Accounting") { 
 
    return d1; 
 
    } 
 
    }); 
 
    if(d.Value.length > 0){ 
 
    return d; 
 
    } 
 
}); 
 
console.log(s);

+0

这将无法正常工作,因为我也在使用相同的功能,但如果您检查数据未经过筛选。 –

+0

@AyoushKohli尝试运行它在这里工作的片段,也忘了返回内部结果我正在使用一些和你正在使用过滤器这些是区别 –

+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) => { 
 
\t \t return item.Value.filter((Initem) => { 
 
\t \t \t return (Initem.Industry == "Accounting") //Filter for Accounting industry 
 
\t }) 
 
}) 
 

 
console.log(test);

+0

感谢您的建议。 但是,这不会工作,因为我也使用相同的,但数据没有得到过滤。 –

+0

@AyoushKohli这是一个工作示例。我不知道你怎么能说它不工作? –