簡體   English   中英

過濾具有多個條件的復雜數組

[英]Filter an complex array with multiple criteria

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 方法來完成。

您可以使用過濾器方法。 不知道你是如何使用它的,但它應該是這樣的。

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

您可以使用帶有搜索條件的數組,並通過檢查鍵和值來過濾數組。

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

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

你可以做:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM