gpt4 book ai didi

javascript - 使用 lodash 基于嵌套数组项过滤数据

转载 作者:行者123 更新时间:2023-11-30 13:54:50 26 4
gpt4 key购买 nike

我有以下数据,无法过滤

{
"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;
})

但在这两种情况下,我都得到了两个项目而不是一个。提前致谢

最佳答案

您可以使用 Lodash 通过在 callback object 中定义匹配的结构来做到这一点像这样:

let d = {"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}}]}}]}}}}

let e = d.data.Viewer.CampaignGrids.edges;
let a = _.filter(e, { node: { Promoted_Titles: [{ Book: { Active: false } }] } });

console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

关于javascript - 使用 lodash 基于嵌套数组项过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57511106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com