I have the following array:
const arr = [
{
_id: '60c936bca0bd431287ae698b',
name: 'Markets',
referential: '60c936bca0bd431287ae6988'
},
{
_id: '60c93b17a0bd431287ae69a0',
name: 'eRetailers',
referential: '60c93b17a0bd431287ae699f'
},
{
_id: '60d0a30d303ebd2a19d6fb4d',
name: 'Products',
referential: '60d0a30d303ebd2a19d6fb4b'
},
{
_id: '60c936bca0bd431287ae6989',
name: 'Regions',
referential: '60c936bca0bd431287ae6988'
},
{
_id: '60d0a30d303ebd2a19d6fb4c',
name: 'Segments',
referential: '60d0a30d303ebd2a19d6fb4b'
}
];
Now I want an array of all the possible combination of the array above, with one condition: a combination cannot contain two objects have the same referential.
Here is the full list of the desired combinations:
Aggregations level 1 :
Products
Segments
Markets
Regions
eRetailers
Aggregations level 2 :
Products/Markets
Products/Regions
Products/eRetailers
Segments/Markets
Segments/Regions
Segments/eRetailers
Markets/Products
Markets/Segments
Markets/eRetailers
Regions/eRetailers
Regions/Products
Regions/Segments
eRetailers/Markets
eRetailers/Regions
eRetailers/Products
eRetailers/Segments
Aggregations Niveau 3 :
Products/Regions/eRetailers
Products/eRetailers/Regions
Segments/Markets/eRetailers
Segments/eRetailers/Markets
Segments/Regions/eRetailers
Segments/eRetailers/Regions
Markets/eRetailers/Segments
Markets/Segments/eRetailers
Regions/Products/eRetailers
Regions/eRetailers/products
Regions/Segments/eRetailers
Regions/eRetailers/PSegments
eRetailers/Regions/Products
eRetailers/Products/Regions
eRetailers/Markets/Segments
eRetailers/Segments/Markets
eRetailers/Regions/Segments
eRetailers/Segments/Regions
Here's the current code which is not working properly:
function combinator (s) {
list_of_strings = new Array();
for(i=0;i<s.length;i++) {
for(j=i+1;j<s.length+1;j++) {
list_of_strings.push(s.slice(i, j));
}
}
return list_of_strings;
}
console.log(combinator(arr));
const result = arr.flatMap( (curVal, idx, myArr) => arr.slice(idx + 1).map(item => ``${curVal.name}/${item.name}) );`