【发布时间】:2017-08-11 08:41:01
【问题描述】:
我正在尝试使用两种过滤方法根据字符串过滤对象内的数组。这是我的对象的样子。
[
{
"cat": "Accommodation and Food Service Activities",
"value": [
{
"sic": "55",
"desc": "Accommodation"
},
{
"sic": "56",
"desc": "Food and beverage service activities"
}
]
},
{
"cat": "Activities Of Extraterritorial Organisations and Bodies",
"value": [
{
"sic": "99",
"desc": "Activities of extraterritorial organisations and bodies"
}
]
}
]
我正在尝试过滤 value.desc 并返回 cat
这是我目前所拥有的
filteredIndustries(industry: string) {
if (industry) {
return this.industries.filter(sector => {
if (sector.value) {
sector.value.findIndex( v => {
return v.desc.toString().toLowerCase().indexOf(industry.toString().toLowerCase()) === 0;
});
}
});
} else {
return this.industries;
}
这是我的html,
<md-input-container>
<input mdInput placeholder="Industry" [mdAutocomplete]="auto" [formControl]="industryCtrl">
</md-input-container>
<md-autocomplete #auto="mdAutocomplete" [displayWith]="displayIndustry.bind(this)">
<md-option *ngFor="let industry of filteredIndustry | async" [value]="industry">
{{ industry.cat }}
</md-option>
</md-autocomplete>
所以当我输入食物时,它应该过滤 value.desc 并返回 cat 应该是 Accommodation and Food Service Activities
但即使我的返回结果为真,我也没有得到任何返回值。
任何帮助将不胜感激。
【问题讨论】:
-
欢迎来到 SO!这是一个很好的第一个问题,您发布了代码并正确解释了您的问题,从而很容易为您提供帮助。 +1
标签: javascript arrays angular typescript