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

ValueError: X has 10 features, but LinearRegression is expecting 1 features as input

So, I am trying to predict the model but its throwing error like it has 10 features but it expacts only 1. So I am confused can anyone help me with it? more importantly its not working for me when my friend runs it. It works perfectly fine dose anyone know the reason about it? cv = KFold(n_splits = 10) all_loss = [] for i in range(9): # 1st for loop over polynomial orders poly_order = i X_train = make_polynomial(x, poly_order) loss_at_order = [] # initiate a set to collect loss for CV for train_index, test_index in cv.split(X_train): print('TRAIN:', train_index, 'TEST:', test_index) X_train_cv, X_test_cv = X_train[train_index], X_test[test_index] t_train_cv, t_test_cv = t[train_index], t[test_index] reg.fit(X_train_cv, t_train_cv) loss_at_order.append(np.mean((t_test_cv - reg.predict(X_test_cv))**2)) # collect loss at fold all_loss.append(np.mean(loss_at_order)) # collect loss at order plt.plot(np.log(al...

Sorting large arrays of big numeric stings

I was solving bigSorting() problem from hackerrank: Consider an array of numeric strings where each string is a positive number with anywhere from to digits. Sort the array's elements in non-decreasing, or ascending order of their integer values and return the sorted array. I know it works as follows: def bigSorting(unsorted): return sorted(unsorted, key=int) But I didnt guess this approach earlier. Initially I tried below: def bigSorting(unsorted): int_unsorted = [int(i) for i in unsorted] int_sorted = sorted(int_unsorted) return [str(i) for i in int_sorted] However, for some of the test cases, it was showing time limit exceeded. Why is it so? PS: I dont know exactly what those test cases were as hacker rank does not reveal all test cases. source https://stackoverflow.com/questions/73007397/sorting-large-arrays-of-big-numeric-stings

How to load Javascript with imported modules?

I am trying to import modules from tensorflowjs, and below is my code. test.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title </head> <body> <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.0.0/dist/tf.min.js"></script> <script type="module" src="./test.js"></script> </body> </html> test.js import * as tf from "./node_modules/@tensorflow/tfjs"; import {loadGraphModel} from "./node_modules/@tensorflow/tfjs-converter"; const MODEL_URL = './model.json'; const model = await loadGraphModel(MODEL_URL); const cat = document.getElementById('cat'); model.execute(tf.browser.fromPixels(cat)); Besides, I run the server using python -m http.server in my command prompt(Windows 10), and this is the error prompt in the console log of my browser: Failed to loa...