【问题标题】:Filter an complex array with multiple criteria过滤具有多个条件的复杂数组
【发布时间】:2019-11-20 23:09:31
【问题描述】:
var sd = {
"searchData": [
    {
        "description": "ISU ISU",
        "tags": {
            "portfolio": [
                "p1",
                "p2",
                "p3"
            ],
            "industry": [
                "i1",
                "i2",
                "i3"
            ]
        },

    },
    {
        "description": null,
        "tags": {
            "portfolio": [
                "p1",
                "p2",
                "p3"
            ],
            "industry": [
                "i1",
                "i4",
                "i5"
            ]
        },
        {
        "description": null,
        "tags": {
            "portfolio": [
                "p4",
                "p5",
                "p6"
            ],
            "industry": [
                "i1",
                "i2",
                "i3"
            ]
        },
    }
]

}

我将从 api 获取这些数据。 我想用 p1 的投资组合价值和 i1 的行业过滤上述内容。

我尝试使用过滤器但无法获得所需的结果。 我不能使用其他库,如 loadash 或 undersore。我必须用普通的 ES6 方法来做。

【问题讨论】:

  • 无法理解您的查询
  • 对不起..这是我在stackoverflow上的第一个Q....我不完全知道如何使用它。
  • 预期输出是什么?
  • “我尝试使用过滤器” ...请显示该尝试。
  • 预期输出是获得具有价值 p1 的投资组合和价值 i1..ie 的行业的对象; searchData 中的前两个对象

标签: javascript arrays reactjs object


【解决方案1】:

您可以使用过滤器方法。不知道你是如何使用它的,但它应该是这样的。

sd.searchData.filter(obj => {
    if (!obj.tags.industry.includes("i1"))
        return false;
    if (!obj.tags.portfolio.includes("p1"))
        return false;
    return true;
})

或者一个班轮:

sd.searchData.filter(obj => obj.tags.industry.includes("i1") && obj.tags.portfolio.includes("p1"));

【讨论】:

    【解决方案2】:

    let sd = {
        searchData : [
            {
                "description": "ISU ISU",
                "tags": {
                    "portfolio": [
                        "p1",
                        "p2",
                        "p3"
                    ],
                    "industry": [
                        "i1",
                        "i2",
                        "i3"
                    ]
                },
    
            },
            {
                "description": null,
                "tags": {
                    "portfolio": [
                        "p1",
                        "p2",
                        "p3"
                    ],
                    "industry": [
                        "i1",
                        "i4",
                        "i5"
                    ]
                },
            },
            {
                "description": null,
                "tags": {
                    "portfolio": [
                        "p4",
                        "p5",
                        "p6"
                     ],
                    "industry": [
                        "i1",
                        "i2",
                        "i3"
                    ]
                },
            }
        ]
        };
    
    console.log(sd.searchData.filter((element) => {
        return element.tags.portfolio.includes('p1') && element.tags.industry.includes('i1')
    }));

    【讨论】:

      【解决方案3】:

      您可以获取带有搜索条件的数组,并通过获取键和值进行检查来过滤数组。

      var data = { searchData: [{ description: "ISU ISU", tags: { portfolio: ["p1", "p2", "p3"], industry: ["i1", "i2", "i3"] } }, { description: null, tags: { portfolio: ["p1", "p2", "p3"], industry: ["i1", "i4", "i5"] } }, { description: null, tags: { portfolio: ["p4", "p5", "p6"], industry: ["i1", "i2", "i3"] } }] },
          search = [["portfolio", "p1"], ["industry", "i1"]],
          result = data.searchData.filter(({ tags }) => search.every(([k, v]) => tags[k].includes(v)));
      
      console.log(result);
      .as-console-wrapper { max-height: 100% !important; top: 0; }

      【讨论】:

        【解决方案4】:

        你可以这样做:

        const sd = {searchData : [{"description": "ISU ISU","tags": {"portfolio": ["p1","p2","p3"],"industry": ["i1","i2","i3"]},},{"description": null,"tags": {"portfolio": ["p1","p2","p3"],"industry": ["i1","i4","i5"]},},{"description": null,"tags": {"portfolio": ["p4","p5","p6"],"industry": ["i1","i2","i3"]},}]};
        const result = sd.searchData.filter(({tags}) => tags.industry.includes('i1') && tags.portfolio.includes('p1'));
        
        console.log(result);

        【讨论】:

          猜你喜欢
          • 2019-08-16
          • 2021-07-05
          • 2019-03-02
          • 1970-01-01
          • 2021-01-26
          • 2021-09-17
          • 1970-01-01
          • 1970-01-01
          • 2021-11-20
          相关资源
          最近更新 更多