I was having some problem when trying to check for duplicate by multiple fields before inserting into the array
. What I am trying to do is retrieve from firebase
, check for accountID
and subtype
fields before inserting into the array
to be resolved by Promise.
What I trying to achieve is If same accountID
, different subtype
, then I add; If same accountID
, same subtype
, I move to next; If different accountID
, different subtype
, I add. Here is my code:
code:
var datasetarr = [];
let promiseKey = new Promise((resolve, reject) => {
for(var i = 0; i < receiptlist.length; i++){
for(var k = 0; k < ritemlist.length; k++){
if(receiptlist[i].date.substring(0, 4) == new Date().getFullYear()){
if(ritemlist[k].receiptID == receiptlist[i].receiptID){
//check duplicate here before insert
if (!datasetarr.find(o => o.accountID === receiptlist[i].accountID && o.subtype === ritemlist[k].type))
datasetarr.push({accountID: receiptlist[i].accountID, subtype: ritemlist[k].type});
}
}
}
}
}
resolve(datasetarr);
});
The part when I tried to print out the array:
array:
promiseKey.then((arr) => {
console.log(arr);
});
The output I am getting:
output:
I still see a lot of duplicate with same accountID and same subtype. Is there anyway to resolve this?
Thanks!