使用多个条件过滤复杂的数组

时间:2019-07-11 09:13:24

标签: javascript arrays reactjs object

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方法来做到这一点。

4 个答案:

答案 0 :(得分: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"));

答案 1 :(得分:1)

您可以使用带有搜索条件的数组,并通过检查键和值来过滤该数组。

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; }

答案 2 :(得分:0)

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 :(得分:0)

您可以这样做:

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);