Javascript 如何筛选数组并添加到新数组
我有一个名为“infos”的对象数组,在这个对象数组中有一个名为TagId的元素。我有另一个名为“TagIds”的数组,它在任何给定时间保存0-3个ID。我想循环遍历我的'infos'数组中的每个对象,如果存在匹配项,则将该对象添加到我的'filteredResults'数组中 现在,我通过以下代码实现了这一点:Javascript 如何筛选数组并添加到新数组,javascript,html,arrays,angular,filter,Javascript,Html,Arrays,Angular,Filter,我有一个名为“infos”的对象数组,在这个对象数组中有一个名为TagId的元素。我有另一个名为“TagIds”的数组,它在任何给定时间保存0-3个ID。我想循环遍历我的'infos'数组中的每个对象,如果存在匹配项,则将该对象添加到我的'filteredResults'数组中 现在,我通过以下代码实现了这一点: infos: Array<{InfoPageId: number, DepartmentId: number, Name: string, Url: string, Html
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;
}
但是,正如您所看到的,TagId是不同的
我希望能够循环使用我的“infos”数组,只向我的“filteredResults”数组添加一次对象,但也只在“TagId”数组中存在每个具有相同“InfoPageId”的对象的TagId时添加该对象。例如,如果“InfoPageId”为8的对象的TagId为12和15,“TagId”数组的值为12和15,则将该对象添加到“filteredResults”数组中。但是,如果该对象仅包含TagId为12但“TagId”数组的值为12和15,则不添加该对象
期望输出的示例
输入:
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"}
]
输出:
filteredResults = [
{InfoPageId: 11, DepartmentId: 1, Name: "Flat Roof Window Buying Guide", Url: "buying-guide-rooflights", Tagid: 10"}
]
希望你们能理解。这很难解释 试试这个:
getFilteredinfos:有吗{
//将每一行减少为包含InfoPageId:[所有标记ID]的一对
让reduced=infos.reduceac,v=>
acc[v.InfoPageId]=[…acc[v.InfoPageId]| |[],v.TagId],acc,{}
;
//查找与每个现有TagId匹配的所有PageID
让FullTagMatchEds=Object.keysreduced.filter
key=>this.TagIds.reduceacc,tag=>acc&&reduced[key]。includestag,true
;
//检索与所有标记ID匹配的每个pageId的第一行
让filteredResults=FullTagMatchEds
.MapInfo=>infos.findinfo=>info.InfoPageId==infoId;
this.infos=filteredResults;
}
看
请注意,输出仅显示其中一个标记,因为这就是您编写输出示例的方式—它只输出包含给定PageInfoId的第一行。如果您希望筛选的对象包含所有匹配的标记ID,我必须稍微更改它
我保留了它们的原样,但我强烈建议您重命名您的变量以符合-不要以大写字母开头的PascalCase命名变量,因为该样式是为类保留的。属性更喜欢使用小写。你真的应该给出输入和所需输出的示例。事实上,我理解你的问题,但没有时间为此编写代码。一般的想法是像使用filter func和indexOf或includes func那样进行操作。之后,我将构建InfoPageId:[TagID]的映射,然后过滤掉那些不符合所需条件的映射。之后,您可以轻松地使用剩余的内容过滤原始数组。希望你得到我的答案,更新了你的建议,例如输入/输出。根据你发布的示例,我假设你希望筛选结果与所有标记ID匹配。是吗?是的,没错。我知道如何删除重复项,但不确定如何做到这一点
filteredResults = [
{InfoPageId: 11, DepartmentId: 1, Name: "Flat Roof Window Buying Guide", Url: "buying-guide-rooflights", Tagid: 10"}
]