【发布时间】:2019-08-15 14:20:53
【问题描述】:
我有以下数据,无法过滤
{
"data": {
"Viewer": {
"CampaignGrids": {
"edges": [
{
"node": {
"Name": "mega campaign",
"Start_Date": "08/31/2020",
"End_Date": "09/15/2020",
"Promoted_Titles": [
{
"Primary_ISBN": "4314323211",
"Book": null
},
{
"Primary_ISBN": "94232344",
"Book": null
},
{
"Primary_ISBN": "221235345",
"Book": null
}
]
}
},
{
"node": {
"Name": "InActiveBook Campaign",
"Start_Date": "08/14/2019",
"End_Date": "08/14/2019",
"Promoted_Titles": [
{
"Primary_ISBN": "9781504011815",
"Book": {
"Primary_ISBN": "9781504011815",
"Active": true
}
},
{
"Primary_ISBN": "9780795336874",
"Book": {
"Primary_ISBN": "9780795336874",
"Active": true
}
},
{
"Primary_ISBN": "9781453244517",
"Book": {
"Primary_ISBN": "9781453244517",
"Active": true
}
},
{
"Primary_ISBN": "9781781892527",
"Book": {
"Primary_ISBN": "9781781892527",
"Active": false
}
}
]
}
}
]
}
}
}
}
我需要过滤所有包含 Active : False book 的广告系列
所以基于以上数据,我需要重新关注
{"node": {
"Name": "InActiveBook Campaign",
"Start_Date": "08/14/2019",
"End_Date": "08/14/2019",
"Promoted_Titles": [
{
"Primary_ISBN": "9781504011815",
"Book": {
"Primary_ISBN": "9781504011815",
"Active": true
}
},
{
"Primary_ISBN": "9780795336874",
"Book": {
"Primary_ISBN": "9780795336874",
"Active": true
}
},
{
"Primary_ISBN": "9781453244517",
"Book": {
"Primary_ISBN": "9781453244517",
"Active": true
}
},
{
"Primary_ISBN": "9781781892527",
"Book": {
"Primary_ISBN": "9781781892527",
"Active": false
}
}
]
}
}
目前我正在使用 lodash 库过滤器选项。我尝试关注,但它返回了两个数据而不是一个
let flaged = _.filter(campaigns, function(item) {
return _.filter(item.Promoted_Titles, function(o) {
return o.Book.Active = false;
})
});
console.log('flagged campaign ', JSON.stringify(flaged) , '\n');
我也试过了
let flaged = filter(campaigns, function(el) {
el.Promoted_Titles = filter(el.Promoted_Titles, function(item) {
console.log('item is ', item);
return 'Book.Active' == false
})
return el;
})
但在这两种情况下,我都得到了两个项目而不是一个。提前致谢
【问题讨论】:
-
所以您希望过滤给定广告系列中的广告系列或图书?看起来您有一个活动数组和一个嵌套的书籍数组,那么您是否要过滤没有活动书籍的活动?或至少 1 本书?
-
我正在尝试过滤至少有一本非活动图书的广告系列
-
o.Book.Active = false是赋值,不是相等检查,'Book.Active' == false总是false,嵌套两个_.filter()调用总是会导致外部回调返回一个真实值(即实际上是外部数组的浅拷贝)
标签: javascript lodash