簡體   English   中英

Javascript如何有效地過濾和訪問數組數組?

[英]Javascript how to efficiently filter and access an array of arrays?

我有一個這種格式的數組

    var data = [
                 "Author Name: Employee1"
               ],
               [
                 "Earnings",
                 "Amount",
                 "Deductions",
                 "Amount",
                 "Take Home"
               ],
               [
                 "Regular",
                 "2,500.00",
                 "Coop Loan",
                 "1,500.00"
               ],
                [
                 "Author Name: Employee2"
               ],
               [
                 "Earnings",
                 "Amount",
                 "Deductions",
                 "Amount",
                 "Take Home"
               ],
               [
                 "Regular",
                 "3,500.00",
                 "Coop Loan",
                 "1,500.00"
               ],

我正在嘗試獲取Regular Array旁邊的值,即 Amounts ["2,500","3,500"]

我首先做的是過濾數組,

$.each(data, function(a,b){ // loop 1
    earnings_list = b.filter(function(z){ 
        if(/^Regular/.test(z) == true) 
        {
            $.each(data, function(x,y){ // loop 2
                 if(x == a) // compare if keys matches
                 {
                   console.log("key: "+a+"  amount: "+y[1]);
                 }
            });
         }
     });  
}); 

輸出是

key: 2 amount: 2,500
key: 5 amount: 3,500

在這個循環中,我能夠獲得常規amount ["2,500"",3,500"]但是,我在重復自己,我有兩個具有相同數據的循環只是為了比較它的鍵,

我有成百上千的字段和記錄要過濾,可能會減慢處理速度,我必須對此進行返工,

我希望有人可以給我建議如何在不重復我的循環的情況下優化我的代碼,謝謝,!

來自評論

我的預期輸出是一個數組,顯示作者姓名:用戶和作者姓名:User2

您可以使用reduce (以獲得扁平數組)和filter

var search = "Author Name";
var output = arr.reduce( ( a, c ) => a.concat(c), [] ).filter( s => s && s.indexOf( search ) != -1 );

演示

 var arr = [ [ "Requirements List" ], [ "System Name: TEST" ], [ "Program/Module Name: TESTs" ], [ "Author Name: USER", null, "Sap Number: 7774" ], [ "Earnings", "Amount", "Deductions", "Amount", "Take Home" ], [ "Regular", "2,500.00", "Coop Loan", "1,500.00" ], [ "Overtime", "1,500.00", "Tax", "2,200.00" ], [ "Holidays", "700.00", "Gym", "0.00" ], [ "Bonus", "1,000.00" ], [ "Birthday", "0.00" ], [ "Total Earnings", "5,700.00", "Total Deduction", "3,700.00", "2,000.00" ], [ "System Name: ETEASDASr" ], [ "Program/Module Name: EAASDA" ], [ "Author Name: USER2", null, "Sap Number: 7774" ], [ "Earnings", "Amount", "Deductions", "Amount", "Take Home" ], [ "Regular", "3,500.00", "Coop Loan", "1,500.00" ], [ "Overtime", "1,500.00", "Tax", "2,200.00" ], [ "Holidays", "700.00", "Gym", "0.00" ], [ "Bonus", "1,000.00" ], [ "Birthday", "0.00" ], [ "Total Earnings", "6,700.00", "Total Deduction", "3,700.00", "3,000.00" ], ]; var search = "Author Name"; var output = arr.reduce((a, c) => a.concat(c), []).filter(s => s && s.indexOf(search) != -1); console.log(output);

您可以使用 reduce 來獲取您想要的值,我已經減少了示例數據。 將正則表達式存儲為變量可能有助於提高性能:

 var data = [ [ "Requirements List" ], [ "Author Name: USER", null, "Sap Number: 7774" ], [ "Regular", "3,500.00", "Coop Loan", "1,500.00" ], [ "Author Name: USER2", null, "Sap Number: 7774" ] ] // Loop over contents of data var authors = data.reduce(function(acc, arr) { // Get elements starting with "Author Name" and append to accumulator return acc.concat(arr.filter(function(s){return /^Author Name:/.test(s)})); }, []); console.log(authors); // Using arrow functions: var authors2 = data.reduce((acc, arr) => acc.concat(arr.filter(s => /^Author Name:/.test(s))), []); console.log(authors2);

您可以通過檢查項目來采用迭代和遞歸方法,如果它是一個數組,則調用然后獲取數組的回調。 如果沒有,請檢查該值並將其連接到結果集(如果包含)。

 var data = [["Requirements List"], ["System Name: TEST"], ["Program/Module Name: TESTs"], ["Author Name: USER", null, "Sap Number: 7774"], ["Earnings", "Amount", "Deductions", "Amount", "Take Home"], ["Regular", "2,500.00", "Coop Loan", "1,500.00"], ["Overtime", "1,500.00", "Tax", "2,200.00"], ["Holidays", "700.00", "Gym", "0.00"], ["Bonus", "1,000.00"], ["Birthday", "0.00"], ["Total Earnings", "5,700.00", "Total Deduction", "3,700.00", "2,000.00"], ["System Name: ETEASDASr"], ["Program/Module Name: EAASDA"], ["Author Name: USER2", null, "Sap Number: 7774"], ["Earnings", "Amount", "Deductions", "Amount", "Take Home"], ["Regular", "3,500.00", "Coop Loan", "1,500.00"], ["Overtime", "1,500.00", "Tax", "2,200.00"], ["Holidays", "700.00", "Gym", "0.00"], ["Bonus", "1,000.00"], ["Birthday", "0.00"], ["Total Earnings", "6,700.00", "Total Deduction", "3,700.00", "3,000.00"]], search = 'Author Name', result = data.reduce(function iter(r, a) { return Array.isArray(a) ? a.reduce(iter, r) : r.concat(('' + a).includes(search) ? a : []); }, []); console.log(result);

暫無
暫無

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

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