2017-07-26 150 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這是一個工作示例。我不知道你怎麼能說它不工作? –