I have 3 arrays and I need to have a final array that contains the unique elements. Currently, I am able to get the "right" results BUT I'm not able to get the array to have the value that I want. I need the array to have the sys_id but the sys_id is unique so I can't find duplicates the way I can with name. This code gives me accurate information.
const cert = [];
const certified = [];
const pending = [];
const failedci = ['Name0','Name1','Name2','Name3','Name4','Name6','Name7','Name8'];
const pendingci = ['Name1','Name2','Name3','Name4','Name6','Name7','Name8','Name0','Name1','Name2','Name3','Name4','Name6','Name7','Name8','Name0','Name1','Name2','Name3','Name4','Name6','Name7','Name8','Name0'];
const certifiedci = ['Name1','Name2','Name3','Name4','Name5','Name6','Name7','Name8','Name9','Name0','Name1'];
// Compare and filter the arrays
for(var i=0; i < pendingci.length; i++){
if(failedci.indexOf(pendingci[i]) == -1){
pending.push(pendingci[i]);
}
}
for(var i=0; i<certifiedci.length; i++){
if(failedci.indexOf(certifiedci[i]) == -1){
cert.push(certifiedci[i]);
}
}
for(var i=0; i<cert.length; i++){
if(pending.indexOf(cert[i]) == -1){
certified.push(cert[i]);
}
}
console.log('======================================================= FAILED ============================================================');
console.log(failedci.length);
console.log(failedci.join('\r\n'));
console.log('======================================================= PENDING ============================================================');
console.log(pending.length);
console.log(pending.join('\r\n'));
console.log('======================================================= CERTIFIED ============================================================');
console.log(certified.length);
console.log(certified.join('\r\n'));
Each "name" even if it is the same name, it is associated with a unique sys_id. I can build the arrays with the sys_id so that it looks like this:
var failedci = [
[Name0,Sys_ID0],
[Name1,Sys_ID1],
[Name2,Sys_ID2],
[Name3,Sys_ID3],
[Name4,Sys_ID4],
[Name6,Sys_ID6],
[Name7,Sys_ID7],
[Name8,Sys_ID8],
[Name1,Sys_ID10]
];
var pendincgi = [
[Name1,Sys_ID11],
[Name2,Sys_ID12],
[Name3,Sys_ID13],
[Name4,Sys_ID14],
[Name6,Sys_ID16],
[Name7,Sys_ID17],
[Name8,Sys_ID18],
[Name0,Sys_ID20],
[Name1,Sys_ID21],
[Name2,Sys_ID22],
[Name3,Sys_ID23],
[Name4,Sys_ID24],
[Name6,Sys_ID26],
[Name7,Sys_ID27],
[Name8,Sys_ID28],
[Name0,Sys_ID30],
[Name1,Sys_ID41],
[Name2,Sys_ID42],
[Name3,Sys_ID43],
[Name4,Sys_ID44],
[Name6,Sys_ID46],
[Name7,Sys_ID47],
[Name8,Sys_ID48],
[Name0,Sys_ID50]
];
var certifiedci = [
[Name1,Sys_ID31],
[Name2,Sys_ID32],
[Name3,Sys_ID33],
[Name4,Sys_ID34],
[Name5,Sys_ID35],
[Name6,Sys_ID36],
[Name7,Sys_ID37],
[Name8,Sys_ID38],
[Name9,Sys_ID39],
[Name0,Sys_ID30],
[Name1,Sys_ID51]
];
How do I compare just the "name" and then have all the unique names' sys_id in one array using the precedence of failed, pending, certified. If failed, it should be removed from all the other arrays. If its not failed but is pending, it should be removed from certified, if it is there. Certified would be what's left that isn't in failed or pending. I don't want to do whatever is left is certified because a new status may be introduced one day.
As I stated, the code properly provides the correct names, I just need to get the specific sys_id that is associated with that element (record) because that is the key.
Here is the outcome from the above script:
======================================================= FAILED ============================================================
8
Name0
Name1
Name2
Name3
Name4
Name6
Name7
Name8
======================================================= PENDING ============================================================
0
======================================================= CERTIFIED ============================================================
2
Name5
Name9
The outcome array should be:
outcome = [
Sys_ID0, //failed
Sys_ID1, //failed
Sys_ID2, //failed
Sys_ID3, //failed
Sys_ID4, //failed
Sys_ID6, //failed
Sys_ID7, //failed
Sys_ID8, //failed
Sys_ID35, //certified
Sys_ID39 //certified
];
There were no pending since all the names that were in the pending array were also in the failed array and failed takes precedence over pending. There were only 2 certified because the rest that were certified were also in failed and failed takes precedence over certified too.
Via Active questions tagged javascript - Stack Overflow https://ift.tt/egcioSD
Comments
Post a Comment