Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript compare two tables

I need help with comparing two tables that presents like this:

table A = [1, 2, 3, 4, 5, 6, 7],
table B = [2, 4]

I need to check if the table A contains numbers from table B and to put it in another array:

Expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

table C = [false, true, false, true, false, false, false]

Tried a few things but nothing works out for me.
The table.includes method gives me following output

let isAdded = [];
  allObjectivesArray.forEach((element, index) => {
    if (allObjectivesArray.includes(reducedArr[0][element - 1])) 
isAdded.push(true);
    else isAdded.push(false);

});

// That is the output of above
isAdded = [true, true, false, false, false, false, false]

Thank you for your help.

>Solution :

You can map the array a and check in the callback if each item exists in the array b with includes function, if exists includes will return true if not will return false

const a = [1, 2, 3, 4, 5, 6, 7];
const b = [2, 4];

const result = a.map(i => b.includes(i))

console.log(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading