循环并比较javascript中的两个嵌套数组

时间:2014-06-23 15:18:19

标签: javascript arrays for-loop comparison

击中有两个嵌套数组的墙,并想要遍历第一个数组,并将每个嵌套数组与另一个数组中的每个嵌套数组进行比较。如果匹配副本数组到第3个数组。例如:

var bills = [
 ["1/1/2013","Bill",0,0,"Fake Management","Management Fee","","$750.00",0,0,"$19,148.85"],
 ["1/1/2013","Bill",0,0,"Fake Edison","Electric PLP","","$1,208.37",0,0,"$20,357.22"],
 ["1/1/2013","Bill",0,0,"Fake Elevator","Monthly Elevator Maintenance January","","$1,055.27",0,0,"$21,412.49"],
 ["1/2/2013","Bill",0,0,"Fake Rug Repair Service","5 Floor, wall to wall carpet","","$375.00",0,0,"$21,787.49"],
];

var payments = [
["1/3/2013","Check EFT",0,0,"Carlos ","Weekly Cleaning","$375.00","",0,0,"$21,124.29"],
["1/4/2013","Check 126",0,0,"Fake Edison","25-2658-0826-0000-8 - Electric PLP","$1,208.37","",0,0,"$19,915.92"],
["1/4/2013","Check 128",0,0,"Fake Rug Repair Service","5 Floor, wall to wall carpet","$375.00","",0,0,"$19,540.92"],
["1/4/2013","Check 129",0,0,"Fake Group Companies","CUC-7001484-02-01 - Insurance First Payment","$260.50","",0,0,"$19,280.42"],
];

var paid = [];

var unpaid = [];

以下是我尝试做的伪代码:

for(var i = 0; i < bills.length; i++) {
if (bills[i][5] ==== AN ARRAY[5] INSIDE payments)
paid.push(bills[i])
} else {
    unpaid.push(bills[i])
}
}

&#34; ANRAY [5] INSIDE付款&#34;是纯伪代码,如果它可以被翻译成JS,将会有所帮助。我认为每个账单阵列都需要遍历每个支付阵列,但是如何实施的逻辑正在躲避我。

2 个答案:

答案 0 :(得分:1)

目前尚不清楚真正的比较标准是什么。这是一个按价格比较的例子(如$ 375.00)。注意数组索引。 [5]完全没有意义(索引是从零开始的)。

var bills = [
["1/1/2013","Bill",0,0,"Fake Management","Management Fee","","$750.00",0,0,"$19,148.85"],
["1/1/2013","Bill",0,0,"Fake Edison","Electric PLP","","$1,208.37",0,0,"$20,357.22"],
["1/1/2013","Bill",0,0,"Fake Elevator","Monthly Elevator Maintenance January","","$1,055.27",0,0,"$21,412.49"],
["1/2/2013", "Bill" ,0 , 0, "Fake Rug Repair Service", "5 Floor, wall to wall carpet", "" , "$375.00", 0, 0, "$21,787.49" ],
["1/4/2013","Check 128", 0, 0, "Fake Rug Repair Service", "5 Floor, wall to wall carpet", "$375.00", "", 0, 0, "$19,540.92" ],
];

var payments = [
["1/3/2013","Check EFT",0,0,"Carlos ","Weekly Cleaning","$375.00","",0,0,"$21,124.29"],
["1/4/2013","Check 126",0,0,"Fake Edison","25-2658-0826-0000-8 - Electric PLP","$1,208.37","",0,0,"$19,915.92"],
["1/4/2013","Check 128", 0, 0, "Fake Rug Repair Service", "5 Floor, wall to wall carpet", "$375.00", "", 0, 0, "$19,540.92" ],
["1/4/2013","Check 129",0,0,"Fake Group Companies","CUC-7001484-02-01 - Insurance First Payment","$260.50","",0,0,"$19,280.42"],
];

var paid = [];
var unpaid = [];

var i, j, bLen = bills.length, pLen = payments.length;
var bill, payment, foundPaid;

for( i = 0; i < bLen; i += 1 ) {
    bill = bills[i];
    foundPaid = false;

    for ( j = 0; j < pLen; j += 1 ) {
        payment = payments[j];

        if ( bill[7] === payment[6] ) {
            foundPaid = true;
            break;
        }
    }

    if ( foundPaid ) {
        paid.push( bill );
    } else {
        unpaid.push( bill );
    }
}

console.log( 'PAID', paid );
console.log( 'UNPAID', unpaid );

将此仅作为如何在javascript中循环的示例。 小提琴:http://jsfiddle.net/qk8N3/

答案 1 :(得分:0)

修改添加了JS小提琴:http://jsfiddle.net/cDVvD/

我发现在这种情况下使用Underscore.js最简单。你可以这样做:

var paid = [];
var unpaid = [];
_.each(bills, function(bill) {
    var isPaid = _.find(payments,function(payment) { return payment[5] === bill[5]});
    if (isPaid) {
        paid.push(bill);
    } else {
        unpaid.push(bill);
    } 
});

如果不能包含外部库,则可以使用嵌套的forEach循环。像

这样的东西
var paid = [];
var unpaid = [];
bills.forEach(function(bill) {
    var isPaid = false;
    payments.forEach(function(payment) {
       if (payment[5] === bill[5]) isPaid = true;
    });
    if (isPaid) {
        paid.push(bill);
    } else {
        unpaid.push(bill);
    } 
});

如果您愿意,也可以使用for循环构造。