Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

427
Views
What can be used to identify the source collection for common elements from n-number of collections?

Given n-number of collections/arrays, I would like to identify common elements and which collections they are common to.

This is a little bit similar to this question, however there could be similar elements in say, colletion1 and collection3, or even all of them. I am not only looking for elements similar in all collections. In addition I am open to using any Node.js libraries.

var arr["One"] =   arrProps[{name: '1', prop2: 'aaa'}], arrValues['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco', 'pizza'];
var arr["Two"] =   arrProps[{name: '2', prop2: 'bbb'}], arrValues['taco', 'fish', 'apple', 'pizza', 'car'];
var arr["Three"] = arrProps[{name: '3', prop2: 'ccc'}], arrValues['banana', 'pizza', 'fish', 'apple', 'orange', ];
var arr["Four"] =  arrProps[{name: '4', prop2: 'ddd'}], arrValues['grape', 'pear', 'chicken', 'car', 'orange'];

Result should be:

[arrValue, arrProps.name]
apple: 1,2,3
banana: 1,3
pear: 1,4
fish: 1,2,3
taco:  1,2
pizza: 1,3
car: 2,4
orange: 3,4

Rather than using arrays, if this were represented as a JSON graph, would that be any easier to solve, and if so, how?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Thanks to @FFire for tackling this.

I have modified the code in order to be able to access object properties and output the result in various tables for visibility.

const arr = [
{ arrProps: [{name: '1', prop2: 'aaa'}], arrValues: ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco', 'pizza'] },
{ arrProps: [{name: '2', prop2: 'bbb'}], arrValues: ['taco', 'fish', 'apple', 'pizza', 'car'] },
{ arrProps: [{ name: '3', prop2: 'ccc' }], arrValues: ['banana', 'pizza', 'fish', 'apple', 'orange'] },
{ arrProps: [{ name: '4', prop2: 'ddd' }], arrValues: ['grape', 'pear', 'chicken', 'car', 'orange'] },
{ arrProps: [{ name: '5', prop2: 'ddeeed' }], arrValues: ['blade', 'movie', 'apple', 'banana', 'lake'] }

];

const result = arr
  .flatMap(({ arrValues }) => arrValues )  // all values
  .filter((value, index, coll) => coll.indexOf(value) === index) //unique values
  .reduce((acc, value) => {
    const parentProp = arr      // name, prop2, arrValues
      .filter((obj) => obj.arrValues.includes(value))
      .map((obj) => obj.arrProps[0]);
      
      //acc[value] = (acc[value] ? [...acc[value], parentProp] : [parentProp])
      //.join(',');
      acc[value] = parentProp
      
    return acc;
}, {})


console.log(result);


Object.entries(result).forEach(([k, v]) => {
    //console.log("The pair: ", k)
    ////console.log("The value: ", v)
    //v.forEach(_parent => console.log(`
    //    Name: ${_parent.name}    
    //    Prop: {_parent.prop2}
    //`));
    console.log(`      ----] The value '${k}' exists in:`);
    console.table(v);
})

console.table(result);

//result["apple"].forEach(e => console.log(e.name));

console.log(JSON.stringify(result));

enter image description here

enter image description here

Of course, like all solutions, there may be a more efficient way of doing this. More responses welcome.

about 4 years ago · Santiago Trujillo Report

0

Kind of 'dumb' solution:

const arr = [
{ arrProps: [{name: '1', prop2: 'aaa'}], arrValues: ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco', 'pizza'] },
{ arrProps: [{name: '2', prop2: 'bbb'}], arrValues: ['taco', 'fish', 'apple', 'pizza', 'car'] },
{ arrProps: [{name: '3', prop2: 'ccc'}], arrValues: ['banana', 'pizza', 'fish', 'apple', 'orange' ] },
{ arrProps: [{name: '4', prop2: 'ddd'}], arrValues: ['grape', 'pear', 'chicken', 'car', 'orange'] }
];

const result = arr
  .flatMap(({ arrValues }) => arrValues )  // all values
  .filter((value, index, coll) => coll.indexOf(value) === index) //unique values
  .reduce((acc, value) => {
    const propName = arr
      .filter((obj) => obj.arrValues.includes(value))
      .map((obj) => obj.arrProps[0].name);
      
    acc[value] = (acc[value] ? [...acc[value], propName] : [propName])
      .join(',');
      
    return acc;
}, {})

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }


update

I reconsidered my approach to solution and realized it was fundamentally wrong.

updated solution:

const arr = [
{ arrProps: [{name: '1', prop2: 'aaa'}], arrValues: ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco', 'pizza'] },
{ arrProps: [{name: '2', prop2: 'bbb'}], arrValues: ['taco', 'fish', 'apple', 'pizza', 'car'] },
{ arrProps: [{name: '3', prop2: 'ccc'}], arrValues: ['banana', 'pizza', 'fish', 'apple', 'orange' ] },
{ arrProps: [{name: '4', prop2: 'ddd'}], arrValues: ['grape', 'pear', 'chicken', 'car', 'orange'] }
];

const result = arr.reduce((acc, { arrProps: [{ name }], arrValues }) => {
    arrValues.forEach((value) => {
         acc[value] = acc[value] ? `${acc[value]},${name}`: name;
    });     
    return acc;
}, {});

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!