I am new to Angular and Coding.
I currently have this line of code that is filtering my brands in my json. The piece of code that is m.Properties. Basically what the code below does is its looking at the brands and filtering the brands where the properties of the brand are the same as the strings in the filteredProperties array.
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));
}
}
However right now my JSON looks like this, note it is hard coded into my service:
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"],
},
]
}
}
My Model looks like this:
export class Brands {
Id: number;
Title: string;
Description: string;
ImageUrl: string;
FundsUrl: string;
OurPeopleUrl: string;
WhyInvestUrl: string;
MoreInfoUrl: string;
Properties: string[];
}
So as you can see the problem that occurs is that it returns undefined when I select m.Properties. I am using an input checkbox to filter. I am not sure how to proceed.
Thank you so much in advance.
EDIT: Here is my input box code:
<input type="checkbox" name="Personal" value="Personal" [(ngModel)]="Personal" (change)="checkValue(Personal, property='personal')"/> Personal<br />
So what happens is when the input box is clicked, the property is added to the array filteredProperties. These are the properties that are currently being looked for in the JSON properties.
EDIT 2:
So I was able to use part of @trichetriche answer and use indexOf to get a result.
My Code:
this.visibleBrands = this.brands.filter(item => item.Properties.some(property => this.filteredProperties.indexOf(property) > -1));