【发布时间】:2018-03-04 00:57:03
【问题描述】:
我正在尝试循环和过滤由不同数组和对象组成的复杂数据结构,如下所示:
// current tags to filter by
let filterTags = ['Folk', 'Professional']
// return the same data structure that has filtered out any events that dont
// have SecondaryTag.Title in the filterTags
let filteredEvents = allEvents.filter((el) => {
return filterTags.includes(el)
});
但是,数据结构由复杂的数组和对象组成,这使得这非常复杂。
我已经尝试了各种各样的事情,这让我把头撞到了我的键盘上。与其发布我尝试过的所有荒谬的事情,我想我会在这里发布我正在努力完成的事情,希望有好心人能帮助我。
let allEvents = [{
'2018': {
'03': {
'31': [
{
ID: 1,
Title: "My Project",
Date: "2018-02-27",
SecondaryTag: {
Title: "Professional"
}
}
],
'28': [
{
ID: 2,
Title: "My Project",
Date: "2018-02-27",
SecondaryTag: {
Title: "Business & Professional"
}
}
]
},
'04': {
'12': [
{
ID: 5,
Title: "My Project2",
Date: "2018-04-12",
SecondaryTag: {
Title: "concert"
}
}
],
'2': [
{
ID: 7,
Title: "My Project2",
Date: "2018-04-12",
SecondaryTag: {
Title: "Folk"
}
}
]
}
}
}];
// This would be the returned filtered structure given the tags
[{
'2018': {
'03': {
'31': [
{
ID: 1,
Title: "My Project",
Date: "2018-02-27",
SecondaryTag: {
Title: "Professional"
}
}
],
},
'04': {
'2': [
{
ID: 7,
Title: "My Project2",
Date: "2018-04-12",
SecondaryTag: {
Title: "Folk"
}
}
]
}
}
}]
【问题讨论】:
-
欢迎来到 StackOverflow!我有一个问题,当你通过
['Folk', 'Professional']过滤时,你想要Business & Professional吗? -
您好,我没有,感谢您的快速回复
标签: arrays filter ecmascript-6 javascript-objects