【发布时间】:2018-08-30 08:10:55
【问题描述】:
我是 Angular 和编码的新手。
我目前有这行代码在我的 json 中过滤我的品牌。 m.Properties 这段代码。基本上下面的代码所做的是查看品牌并过滤品牌属性与filteredProperties数组中的字符串相同的品牌。
for (var i = this.filteredProperties.length - 1; i >= 0; i--) {
if (this.filteredProperties[i] == property) {
this.visibleBrands = this.brands.filter(m => m.Properties == this.filteredProperties.find(y => y === m.Properties));
}
}
但是现在我的 JSON 看起来像这样,请注意它是硬编码到我的服务中的:
import { Injectable } from '@angular/core';
import { Brands } from '../models/brand.model';
@Injectable()
export class BrandService {
getBrands(): Brands[] {
return [
{
"Id": 1,
"Title": "Alternative Investments",
"Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sollicitudin ornare lectus, quis gravida tortor venenatis nec.",
"ImageUrl": "C:/Users/xy57418/Pictures/Anguar Rewrite/PlaceHolder BrandLogos.png",
"FundsUrl": "www.google.com",
"OurPeopleUrl": "www.google.com",
"WhyInvestUrl": "www.google.com",
"MoreInfoUrl": "www.google.com",
"Properties": ["institutional"],
},
{
"Id": 2,
"Title": "Customised Solutions",
"Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sollicitudin ornare lectus, quis gravida tortor venenatis nec. ",
"ImageUrl": "C:/Users/xy57418/Pictures/Anguar Rewrite/PlaceHolder BrandLogos.png",
"FundsUrl": "www.google.com",
"OurPeopleUrl": "www.google.com",
"WhyInvestUrl": "www.google.com",
"MoreInfoUrl": "www.google.com",
"Properties":["personal"],
},
{
"Id": 3,
"Title": "Future Growth",
"Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sollicitudin ornare lectus, quis gravida tortor venenatis nec. ",
"ImageUrl": "C:/Users/xy57418/Pictures/Anguar Rewrite/PlaceHolder BrandLogos.png",
"FundsUrl": "www.google.com",
"OurPeopleUrl": "www.google.com",
"WhyInvestUrl": "www.google.com",
"MoreInfoUrl": "www.google.com",
"Properties": ["institutional"],
},
{
"Id": 4,
"Title": "Global Emerging Markets",
"Description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sollicitudin ornare lectus, quis gravida tortor venenatis nec. ",
"ImageUrl": "C:/Users/xy57418/Pictures/Anguar Rewrite/PlaceHolder BrandLogos.png",
"FundsUrl": "www.google.com",
"OurPeopleUrl": "www.google.com",
"WhyInvestUrl": "www.google.com",
"MoreInfoUrl": "www.google.com",
"Properties": ["institutional", "personal"],
},
]
}
}
我的模型如下所示:
export class Brands {
Id: number;
Title: string;
Description: string;
ImageUrl: string;
FundsUrl: string;
OurPeopleUrl: string;
WhyInvestUrl: string;
MoreInfoUrl: string;
Properties: string[];
}
如您所见,出现的问题是当我选择m.Properties 时它返回未定义。我正在使用输入复选框进行过滤。我不确定如何继续。
非常感谢您。
编辑:这是我的输入框代码:
<input type="checkbox" name="Personal" value="Personal" [(ngModel)]="Personal" (change)="checkValue(Personal, property='personal')"/> Personal<br />
所以当点击输入框时,属性被添加到数组filteredProperties中。这些是当前正在 JSON 属性中查找的属性。
编辑 2:
所以我能够使用@trichetriche 的部分答案并使用indexOf 来获得结果。
我的代码:
this.visibleBrands = this.brands.filter(item => item.Properties.some(property => this.filteredProperties.indexOf(property) > -1));
【问题讨论】:
-
请在stackblitz.io上提供您的完整代码或minimal reproducible example。
-
请提供更多关于'm'的细节以及数据是如何到达的,还有你刚刚发布的完整的json文件吗?
-
不,它位于 Angular 服务中。我将在我的全面服务中进行编辑。
-
您在哪里检索 json?当您执行 console.log(m) 时,输出是什么?