Skip to content

Instantly share code, notes, and snippets.

@joe-oli
Created February 19, 2020 12:38
Show Gist options
  • Save joe-oli/b3c34be880cef96633166848f55c5dad to your computer and use it in GitHub Desktop.
Save joe-oli/b3c34be880cef96633166848f55c5dad to your computer and use it in GitHub Desktop.
Compare arrays return true/false
//test #1: exactly the same.
let myArrayA = [
{periodSeqNo: 30010, period_month :'jan 2020', start_date: '2020-01-01', end_date: '2020-01-31', opening_bal: 350000.11, opening_rate: 3.100, int_amount_charged: 255.10},
{periodSeqNo: 30020, period_month :'feb 2020', start_date: '2020-02-01', end_date: '2020-02-29', opening_bal: 360000.12, opening_rate: 3.200, int_amount_charged: 255.20},
{periodSeqNo: 30030, period_month :'mar 2020', start_date: '2020-03-01', end_date: '2020-03-31', opening_bal: 370000.13, opening_rate: 3.300, int_amount_charged: 255.30},
];
let myArrayB = [
{periodSeqNo: 30010, period_month :'jan 2020', start_date: '2020-01-01', end_date: '2020-01-31', opening_bal: 350000.11, opening_rate: 3.100, int_amount_charged: 255.10},
{periodSeqNo: 30020, period_month :'feb 2020', start_date: '2020-02-01', end_date: '2020-02-29', opening_bal: 360000.12, opening_rate: 3.200, int_amount_charged: 255.20},
{periodSeqNo: 30030, period_month :'mar 2020', start_date: '2020-03-01', end_date: '2020-03-31', opening_bal: 370000.13, opening_rate: 3.300, int_amount_charged: 255.30},
];
/*
let testFound = myArrayB.find(elt => elt.periodSeqNo === 30020); //return element or undefined;
console.log('found:', testFound); */
//IF Objects are same, returns TRUE; else rtn false;
const shallowCompareFN = (obj1, obj2) =>
Object.keys(obj1).length === Object.keys(obj2).length &&
Object.keys(obj1).every(key => obj2.hasOwnProperty(key) && obj1[key] === obj2[key]
);
const compareArraysFN = (arrA, arrB) => {
//1. are array lengths the same?
if (arrA.length != arrB.length)
return false; //exit.. NOT THE SAME, we are done.
else { //lengths are the same, are objects (by PK periodSeqNo equivalent?
//pick one array A or B, loop thru it;
//dont use .map for iterations (cannot break/exit early out of loop); map is best used for obtaining a new transformed array.
let rtnValTF = true;
for( const item of arrA) {
//Given ArrayA, look in arrayB for element satisfying a condition (same Key, in this case)
const foundElt = arrB.find(elt => elt.periodSeqNo === item.periodSeqNo);
if (foundElt === undefined) {
return false; //exit.. A+B are different.
} else {//found, compare objects (i.e. each prop in the objs)
/* console.warn( 'objA:', item);
console.log( 'objB:', foundElt); */
console.log( 'objB:', foundElt);
var trueFalse = shallowCompareFN(item, foundElt)
if (!trueFalse) {
rtnValTF = false;
break; //ON FALSE, we are done, early exit, no need to keep looping.
}
}
}
return rtnValTF;
}
}
let tf = compareArraysFN( myArrayA, myArrayB);
console.log('test#1 - are arrays same?:', tf);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment