簡體   English   中英

Javascript如何過濾匹配關鍵字的數組

[英]Javascript how to filter arrays matching a keyword

我有一個深度嵌套的數組,如下所示:

{[{
    id: "1",
    experts: [
        {instruments: [{guitar: ["John", "Maria"], 
                         piano: ["Hans", "Sarah", "Nancy"] }],
         name: "Columbia Records", published: "1930"
        },
        {instruments: [{guitar: ["Pablo"], 
                        drums: ["Jeb", "Xiao"]} ],
         name: "Atlas Records", published: "1978"
        }
    ]
]},
{
    id: "2",
    experts: [
        {instruments: [{sitar: ["Anaka", "Reha"], 
                        chello: ["Hans", "Raphael", "Stuart"]} ],
         name: "Touchstone Records", published: "1991"
        },
        {instruments: [{viola: ["Richard", "Farah"], 
                        buss: ["Tina", "Max"]} ],
         name: "Atlas Records", published: "1978"
        }
    ]

}

我需要執行分級搜索。 例如,如果我的搜索鍵是“Jo”,結果應該是:

[{
        id: "1",
        experts: [{"instruments": [
                                   "guitar": ["John"]
                                  ]              
                 ],
             "name": "Columbia Records", "published": "1930"
 }]

換句話說,每個嵌套級別,我只是根據鍵進行過濾,如果它不存在,則刪除嵌套對象並進一步。 如果搜索鍵是“1”,它將返回整個第一個對象。

我正在研究 loadsh 及其過濾器函數,它采用謂詞但不確定如何實現搜索任意嵌套。

這里看起來很有趣。 首先你的對象聲明有很多錯誤。 您缺少結束標記,並且您沒有使用["key":"value"]類的鍵創建數組,這是用於對象{"key":"value"}

var testArray = [{
    id: "1",
    experts: [
        {"instruments": {"guitar": ["John", "Maria"], 
                         "piano": ["Hans", "Sarah", "Nancy"] },
         "name": "Columbia Records", "published": "1930"
        },
        {"instruments": {"guitar": ["Pablo"], 
                         "drums": ["Jeb", "Xiao"] },
         "name": "Atlas Records", "published": "1978"
        }
    ]
},
{
    id: "2",
    experts: [
        {"instruments": {"sitar": ["Anaka", "Reha"], 
                         "chello": ["Hans", "Raphael", "Stuart"] },
         "name": "Touchstone Records", "published": "1991"
        },
        {"instruments": {"viola": ["Richard", "Farah"], 
                         "buss": ["Tina", "Max"] },
         "name": "Atlas Records", "published": "1978"
        }
    ]
}];

一種方法是使用遞歸,例如:

function deepSearchForValue(obj, searchstring) {
  var doesValueExist = function(item) {
    if (typeof item === "string") {
      return item.toLowerCase().indexOf(searchstring.toLowerCase()) >= 0;
    } else {
      return doesValueExistRecursive(item);
    }
  }

  var doesValueExistRecursive = function(item) {
     for (i in item) { 
        if (doesValueExist(item[i])) {
           return true;
        }
     }
     return false;
  }

  return obj.filter(doesValueExistRecursive);
}

你會這樣稱呼它

var matchedItems = deepSearchForValue(testArray, "reh");

console.log("found:");
console.log(matchedItems);

你可能想要這樣的東西。 算法中的一些修改可能會為您提供您所追求的內容:

 var arr = [ { id: "1", experts: [ { "instruments": { "guitar": ["John", "Maria"], "piano": ["Hans", "Sarah", "Nancy"] }, "name": "Columbia Records", "published": "1930" }, { "instruments": { "guitar": ["Pablo"], "drums": ["Jeb", "Xiao"] }, "name": "Atlas Records", "published": "1978" } ] }, { id: "2", experts: [ { "instruments": { "sitar": ["Anaka", "Reha"], "chello": ["Hans", "Raphael", "Stuart"] }, "name": "Touchstone Records", "published": "1991" }, { "instruments": { "viola": ["Richard", "Farah"], "buss": ["Tina", "Max"] }, "name": "Atlas Records", "published": "1978" } ] }]; var t = _.remove(arr, function(n) { if(!_.includes(n, 'Jo')){ return n; } }); document.getElementById('search').innerHTML = JSON.stringify(t);
 <!DOCTYPE html> <html> <head> <script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script> <meta charset="utf-8"> <title>JS Bin</title> </head> <body> <div id="search"></div> </body> </html>

暫無
暫無

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

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