-1

Hitting a wall where there are two nested arrays and want to loop through the first array and compare every nested array to every nested array in the other array. If there is a match copy array to a 3rd array. Example:

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 = [];

Here is the Pseudo code of what I'm trying to do:

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])
}
}

"AN ARRAY[5] INSIDE payments" is pure pseudocode and would be helpful if it could be translated into JS. I'm thinking that each bills array would need to loop through every payments array but the logic of how to implement is evading me.

Evan Davis
  • 35,493
  • 6
  • 50
  • 57
Barry G
  • 221
  • 4
  • 14
  • 1
    You want to compare each item in `bills` to each item in `payments`? Nested loop. – Evan Davis Jun 23 '14 at 15:34
  • Try a function that you call with the `bills[i][5]` and the `payments` array. What do you think would that function look like? – Bergi Jun 23 '14 at 15:38

2 Answers2

1

It was unclear what is the real comparison criteria. Here is an example when comparing by price (like $375.00). Pay attention to array indexes. [5] made no sense at all (Indexes are zero-based).

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

Take this solely as an example of how to loop in javascript. Fiddle: http://jsfiddle.net/qk8N3/

Ingmars
  • 998
  • 5
  • 10
  • array[5] would mean the 6th entry in any array that matches bills[i][5] – Barry G Jun 23 '14 at 17:08
  • @user2522280 I meant it made no sense in terms of using it as a comparison field - content is freestyle-ish description string as opposed to concrete ID or price or something. – Ingmars Jun 24 '14 at 08:12
0

Edit Added a JS Fiddle: http://jsfiddle.net/cDVvD/

I find it simplest to use Underscore.js in the case. You could do something like this:

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

If you can't include outside libraries, you could use nested forEach loops instead. Something like

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

You could also use your for loop construction, if you prefer that.

Cody Brumfield
  • 487
  • 4
  • 10