【问题标题】:Filter a json dataset by a field 4 levels deep按 4 级深度的字段过滤 json 数据集
【发布时间】:2021-03-09 10:49:21
【问题描述】:

采取如下json结构,如何在下4层的categories节点上进行过滤。

例如,在这个例子中只返回一个包含“cat8”的项目的json对象,我会得到项目A和B?

我正在使用节点并安装了 lodash。

我会在源头正常过滤数据,但我有数据集,我不能这样做。

{
    "items": [
        {
            "fields": {
                "title": "Item A",
                "section1": {
                    "fields": {
                        "title": "Level 2 title",
                        "section2": {
                            "fields": {
                                "title": "Level 3 title",
                                "section3": {
                                    "fields": {
                                        "title": "Level 4 title",
                                        "categories": [
                                            "cat1",
                                            "cat6",
                                            "cat8"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        {
            "fields": {
                "title": "Item B",
                "section1": {
                    "fields": {
                        "title": "Level 2 title",
                        "section2": {
                            "fields": {
                                "title": "Level 3 title",
                                "section3": {
                                    "fields": {
                                        "title": "Level 4 title",
                                        "categories": [
                                            "cat1",
                                            "cat8"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        {
            "fields": {
                "title": "Item C",
                "section1": {
                    "fields": {
                        "title": "Level 2 title",
                        "section2": {
                            "fields": {
                                "title": "Level 3 title",
                                "section3": {
                                    "fields": {
                                        "title": "Level 4 title",
                                        "categories": [
                                            "cat1",
                                            "cat3"
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ]
}

【问题讨论】:

    标签: javascript arrays json lodash


    【解决方案1】:

    使用 lodash 非常简单

    const results = _.filter(deepObject,['path.to.deep.property', expectedValue])
    

    【讨论】:

    • 这已经成功了。我正在使用 where ,但我无法弄清楚如何获得深入的工作路径。现在可以工作了。谢谢
    【解决方案2】:

    在事先不知道结构的情况下,您可以访问所有对象并检查值是否包含想要的值。

    const
        hasCategory = o => Object
            .values(o)
            .some(p => p === category || p && typeof p === 'object' && hasCategory(p)),
        data = { items: [{ fields: { title: "Item A", section1: { fields: { title: "Level 2 title", section2: { fields: { title: "Level 3 title", section3: { fields: { title: "Level 4 title", categories: ["cat1", "cat6", "cat8"] } } } } } } } }, { fields: { title: "Item B", section1: { fields: { title: "Level 2 title", section2: { fields: { title: "Level 3 title", section3: { fields: { title: "Level 4 title", categories: ["cat1", "cat8"] } } } } } } } }, { fields: { title: "Item C", section1: { fields: { title: "Level 2 title", section2: { fields: { title: "Level 3 title", section3: { fields: { title: "Level 4 title", categories: ["cat1", "cat3"] } } } } } } } }] },
        category = 'cat8',
        result = data.items.filter(hasCategory);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 2012-10-13
      • 2018-01-29
      • 2017-09-18
      • 2015-02-20
      相关资源
      最近更新 更多