Skip to main content

How to compare parts of an array in JavaScript?

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

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console