I have an array of objects called 'infos', within this array of objects i have an element called TagId. I have another array called 'TagIds' which holds 0 - 3 ids at any one given time. I want to loop through each object within my 'infos' array and if there is a match add the object to my 'filteredResults' array.
Now i have achieved this by the following code:
infos: Array<{InfoPageId: number, DepartmentId: number, Name: string, Url: string, Html: string, Title: string, Keywords: string, Description: string, InfoTypeId: number, DateCreated: Date, DateUpdated: Date, Hidden: boolean, AMPhtml: string, UseAmp: boolean, UseAmpVideo: boolean, UseCarousel: boolean, TradeOnly: boolean, TagId: number}> = [];
TagIds = [];
getBuyingGuidesNotFiltered() {
this.IsWait = true;
this._SharedService.getAll().subscribe((data: any[]) => {
data.forEach(e => {
if(e.InfoTypeId === 12)
{
this.infos.push(new infoPage(
e.InfoPageId,
e.DepartmentId,
e.Name,
e.Url,
e.Html,
e.Title,
e.Keywords,
e.Description,
e.InfoTypeId,
e.DateCreated,
e.DateUpdated,
e.Hidden,
e.AMPhtml,
e.UseAmp,
e.UseAmpVideo,
e.UseCarousel,
e.TradeOnly,
e.TagId
));
}
})
this.getFiltered(this.infos);
this.IsWait = false;
});
}
getFiltered(infos: Array<infoPage>) {
var filteredResults = []
for(var i = 0; i < this.infos.length; i++) {
for(var j = 0; j < this.TagIds.length; j++) {
if(this.infos[i].TagId === this.TagIds[j]) {
filteredResults.push(this.infos[i]);
}
}
}
this.infos = [];
this.infos = filteredResults;
}
Now the problem is that my object array 'infos' holds duplicate data for example:
this.infos = [
{InfoPageId: 8, DepartmentId: 1, Name: "Pitched Roof Window Buying Guide", Url: "buying-guide-pitched", TagId: 15"}
{InfoPageId: 8, DepartmentId: 1, Name: "Pitched Roof Window Buying Guide", Url: "buying-guide-pitched", TagId: 12"}
]
however as you can see the TagId is different.
I want to be able to loop through my 'infos' array and only add the object once to my 'filteredResults' array but also only add that object when the TagId for each object with the same 'InfoPageId' exists in the 'Tagids' array. for example if the objects with a 'InfoPageId' of 8 have a TagId of 12 and 15 and the 'TagIds' arrays has the values 12 and 15 then add the object to the 'filteredResults' array however if the Object only contains a TagId of 12 but the 'TagIds' array has the values 12 and 15 dont add.
example of desired output;
Input:
TagIds = [10, 5]
infos = [
{InfoPageId: 11, DepartmentId: 1, Name: "Flat Roof Window Buying Guide", Url: "buying-guide-rooflights", Tagid: 10"}
{InfoPageId: 11, DepartmentId: 1, Name: "Flat Roof Window Buying Guide", Url: "buying-guide-rooflights", Tagid: 5"}
{InfoPageId: 12, DepartmentId: 1, Name: "Flat Roof Window Buying Guide", Url: "buying-guide-rooflights", Tagid: 10"}
]
output:
filteredResults = [
{InfoPageId: 11, DepartmentId: 1, Name: "Flat Roof Window Buying Guide", Url: "buying-guide-rooflights", Tagid: 10"}
]
Hope you guys can understand this. Its pretty hard to explain.