How create array containing all combinations of array 1-n array items? - javascript

We have the following task, to convert the array below called passengerFlights (object with passenger-id-keys and array of flights) to:
(1) an array with all possible combination of passenger-flights:
EXPECTED OUTPUT:
[
["aaa", "ddd", "eee"],
["aaa", "ddd", "fff"],
["bbb", "ddd", "eee"],
["bbb", "ddd", "fff"],
["ccc", "ddd", "eee"],
["ccc", "ddd", "fff"]
]
and (2) with the stipulation that there can be any number of passengers.
The following is an attempt to solve this first as a static example of three flights, although it's not clear the best way to (1) create the array with all possible combinations, and (2) how to solve the 2-n requirement, we assume recursion of some kind.
const passengerFlights = {
777: [
{
_id: "aaa"
},
{
_id: "bbb"
},
{
_id: "ccc"
}
],
888: [
{
_id: "ddd"
}
],
999: [
{
_id: "eee"
},
{
_id: "fff"
}
],
};
const getGroupedFlights = (passengerFlights) => {
let indexPointer = 0;
const indexCounters = [0, 0, 0];
const arr = [];
while (indexCounters[0] <= passengerFlights['777'].length - 1 || indexCounters[1] <= passengerFlights['888'].length - 1 || indexCounters[2] <= passengerFlights['999'].length - 1) {
arr.push([passengerFlights['777'][0]._id, passengerFlights['888'][0]._id, passengerFlights['999'][0]._id]);
if (indexCounters[2] < passengerFlights['999'].length) indexCounters[2]++;
if (indexCounters[2] >= passengerFlights['999'].length - 1 && indexCounters[1] < passengerFlights['888'].length) indexCounters[1]++;
if (indexCounters[1] >= passengerFlights['888'].length - 1 && indexCounters[0] < passengerFlights['777'].length) indexCounters[0]++;
console.log(indexCounters, passengerFlights['888'].length - 1);
}
return arr;
}
const groupedFlights = getGroupedFlights(passengerFlights);
console.log(groupedFlights);

It's just a simple recursive problem....
const
passengerFlights =
{ 777: [ { _id: 'aaa' }, { _id: 'bbb' }, { _id: 'ccc' } ]
, 888: [ { _id: 'ddd' } ]
, 999: [ { _id: 'eee' }, { _id: 'fff' } ]
}
, result = combinations( passengerFlights, '_id' )
;
console.log( showArr(result) )
function combinations( obj, KeyName )
{
let
result = []
, keys = Object.keys(obj) // [ "777", "888", "999" ]
, max = keys.length -1
;
f_recursif_combi(0)
return result
function f_recursif_combi( level, arr = [] )
{
obj[ keys[level] ] // like :passengerFlights['777']
.forEach( elm =>
{
let arr2 = [...arr, elm[KeyName] ]; // arr + elm['_id']
(level < max)
? f_recursif_combi(level +1, arr2 )
: result.push( arr2 )
})
}
}
// ************************************ just to present result...
function showArr(Arr)
{
const r = { '[["': `[ [ '`, '","': `', '`, '"],["': `' ]\n, [ '`, '"]]': `' ]\n]` }
return JSON
.stringify(result)
.replace(/\[\[\"|\"\,\"|\"\]\,\[\"|\"\]\]/g,(x)=>r[x])
}
.as-console-wrapper {max-height: 100%!important;top:0 }

I think it's the Cartesian product of flights sets. So this should help:
Cartesian product of multiple arrays in JavaScript

As another answer suggests, you can use a basic cartesian product function. Use Object.values(passengerFlights) to pass in the array of arrays.
function *product(arrs, p = []) {
if (arrs.length == 0)
yield p
else
for (const value of arrs[0])
yield *product(arrs.slice(1), [...p, value])
}
const passengerFlights =
{777: [{_id: "aaa"},{_id: "bbb"},{_id: "ccc"}],888: [{_id: "ddd"}],999: [{_id: "eee"},{_id: "fff"}]}
for (const p of product(Object.values(passengerFlights)))
console.log(JSON.stringify(p.map(obj => obj._id)))
I used JSON.stringify for easy visualization in the demo
["aaa","ddd","eee"]
["aaa","ddd","fff"]
["bbb","ddd","eee"]
["bbb","ddd","fff"]
["ccc","ddd","eee"]
["ccc","ddd","fff"]
But for your program you will probably prefer Array.from
console.log(
Array.from(
product(Object.values(passengerFlights)),
p => p.map(obj => obj._id)
)
)
[
["aaa","ddd","eee"],
["aaa","ddd","fff"],
["bbb","ddd","eee"],
["bbb","ddd","fff"],
["ccc","ddd","eee"],
["ccc","ddd","fff"]
]
Since the order of the expected result is not important, we can make the program more efficient.
function *product(arrs) {
if (arrs.length == 0)
yield []
else
for (const p of product(arrs.slice(1)))
for (const value of arrs[0])
yield [value, ...p]
}
const passengerFlights =
{777: [{_id: "aaa"},{_id: "bbb"},{_id: "ccc"}],888: [{_id: "ddd"}],999: [{_id: "eee"},{_id: "fff"}]}
for (const p of product(Object.values(passengerFlights)))
console.log(JSON.stringify(p.map(obj => obj._id)))
["aaa","ddd","eee"]
["bbb","ddd","eee"]
["ccc","ddd","eee"]
["aaa","ddd","fff"]
["bbb","ddd","fff"]
["ccc","ddd","fff"]

Related

Divide object array elements into groups of n each javascript

I have an Object as below:
const boxOfFruits = {
apples: [
{
name: "Kashmiri",
},
{
name: "Washington",
},
{
name: "Himalayan",
},
{
name: "Fuji",
}
],
oranges: [
{
name: "Nagpur",
},
{
name: "Clementine",
},
],
mangoes: [
{
name: "Totapuri",
},
{
name: "Alphonso",
},
{
name: "Langda",
},
],
}
I want to divide these fruits into boxes; maximum of n each, let's say where n is 3 and apples, oranges and mangoes are equally distributed.
So the output in this case would be:
box_1 = [{name: "Kashmiri"}, {name: "Nagpur"},{name: "Totapuri"}];
box_2 = [{name: "Washington"}, {name: "Clementine"},{name: "Alphonso"}];
box_3 = [{name: "Himalayan"},{name: "Langda"}, {name: "Fuji"}];
The type of fruits(apple,oranges,etc)/keys in object can increase/decrease and n is also variable. In case total fruits are less than n, then it would be just 1 box of fruits.
What I have tried so far:
Using Lodash, I am calculating the minimum and the maximum fruits in a single type:
const minFruitType = _.min(Object.values(basket).map((eachBasket: any) => eachBasket.length));
Total teams will the sum of the fruits / n
Will distribute the minimum fruits (l) in the first l boxes and fill the rest with the remaining fruits at every iteration while at the start of every iteration will calculate the minimum type of fruits again.
You can use Object.values(), array#reduce and array#forEach to transform your object.
const boxOfFruits = { apples: [ { name: "Kashmiri", }, { name: "Washington", }, { name: "Himalayan", }, ], oranges: [ { name: "Nagpur", }, { name: "Clementine", }, ], mangoes: [ { name: "Totapuri", }, { name: "Alphonso", }, { name: "Langda", }, ], },
result = Object.values(boxOfFruits).reduce((r, arr) => {
arr.forEach((o,i) => {
const key = `box_${i+1}`;
r[key] ??= r[key] || [];
r[key].push(o)
});
return r;
},{});
console.log(result);
The easiest way would be to use lodash.js's zip() function:
const boxes = _.zip( Object.values(boxOfFruits) );
Note that _.zip() will give you undefined values when the source arrays are different lengths, so you'll need/want to filter those out:
const boxes == _.zip( Object.values(boxOfFruits) )
.map(
box => box.filter(
x => x !== undefined
)
);
But that will not distribute the fruits evenly. For that, it shouldn't get much for difficult than this:
function distribute(boxOfFruits, n) {
const boxes = [];
const fruits = Object.keys(boxOfFruits);
for ( const fruit of fruits ) {
let i = 0;
const items = boxOfFruits[fruit];
for (const item of items) {
boxes[i] = !boxes[i] ?? [];
boxes[i] = boxes[i].push(item);
++i;
i = i < n ? i : 0 ;
}
}
return boxes;
}
A modified version of #Nicholas Carey's answer worked for me:
function distribute(boxOfFruits, n) {
let boxes = [];
let totalFruits = Object.values(boxOfFruits)
.reduce((content, current) => content + current.length, 0);
let maxBoxes = Math.ceil(totalFruits / 4);
Object.values(boxOfFruits).forEach((fruits) => {
let i = 0;
fruits.forEach((fruit) => {
boxes[i] ??= boxes[i] || [];
boxes[i].push(fruit);
++i;
i = i < (n+1) ? i : 0;
});
});
// Extra boxes created, redistribute them to
// starting boxes
let newBoxes = teams.slice(0, maxBoxes);
let pendingBoxes = teams.slice(maxBoxes);
let pendingFruits = pendingBoxes.flat();
let distributedBoxes = newBoxes.map((eachBox) => {
let required = n - eachBox.length;
if (required > 0) {
eachBox.push(...pendingFruits.splice(0, required));
}
return eachBox;
});
return distributedBoxes;
}
Code is pretty much the same as Nicholas's accept the below changes:
Directly fetched the values and iterated over those
empty array creation was failing, this way works
and checking on the max box size with n+1 instead of n

How to loop inside a map() loop?

The below script outputs
================================================
Log entry ID: 1
UUID: 2
Timestamp: 3
--------------------
Log entry ID: 4
UUID: 5
Timestamp: 6
which is what I want.
Right now description is hard coded, where I would like it to be built using arr instead.
My current thinking were to somehow generate the inner array in the map() function:
[
`Log entry ID: ${element['_id']}`,
`UUID: ${element['_source'].uuid}`,
`Timestamp: ${element['_source']['#timestamp']}\n`,
]
but because of the templating, this is not even a valid array with 3 elements, when looking at it on its own. So I am all out of ideas.
Question
Somehow I have to loop over the elements in arr before it is given to map(), I suppose.
Does anyone know how that could be done?
const dedent = require('dedent');
const arr = [
[ 'Log entry ID', '_id' ],
[ 'UUID', 'uuid' ],
[ 'Timestamp', '#timestamp' ],
]
;
const docs = [
{'_id': 1,'_source': {'uuid': 2,'#timestamp': 3}},
{'_id': 4,'_source': {'uuid': 5,'#timestamp': 6}},
];
const description = dedent`
================================================
${
docs.map((element) => [
`Log entry ID: ${element['_id']}`,
`UUID: ${element['_source'].uuid}`,
`Timestamp: ${element['_source']['#timestamp']}\n`,
].join('\n')).join('--------------------\n')
}
`;
console.log(description);
Update
I control arr so changing it to eg. is possible, or something else
const arr = [
[ 'Log entry ID', '_id' ],
[ 'UUID', {'_source': 'uuid'} ],
[ 'Timestamp', {'_source': '#timestamp'} ],
]
;
Since arr is in your control perhaps you can specify the path of the key itself.
const arr = [
['Log entry ID', '_id'],
['UUID', '_source.uuid'],
['Timestamp', '_source.#timestamp'],
['Description', '_source._desc']
];
const docs = [{
'_id': 1,
'_source': {
'uuid': 2,
'#timestamp': 3,
'_desc': 'test 1'
}
},
{
'_id': 4,
'_source': {
'uuid': 5,
'#timestamp': 6,
'_desc': 'test 2'
}
},
];
const getValue = (object, keys) => keys.split('.').reduce((o, k) => (o || {})[k], object);
console.log(docs.map((element) => arr.map((label) => {
return `${label[0]}: ${getValue(element, label[1])}`
}).join('\n')).join('\n--------------------\n'))
Assuming that the keys of a docs array element are all unique, one could traverse the object looking for a matching key.
function findVal(object, key) {
var value;
Object.keys(object).some(function(k) {
if (k === key) {
value = object[k];
return true;
}
if (object[k] && typeof object[k] === 'object') {
value = findVal(object[k], key);
return value !== undefined;
}
});
return value;
}
docs.map((element) =>
arr.map(([item, key]) =>
`${item}: ${findVal(element, key)}`)
)
The FindVal is taken from here.
you could create a function to get data from arr and put in description
/* Simple Hello World in Node.js */
const arr = [
[ 'Log entry ID', '_id' ],
[ 'UUID', 'uuid' ],
[ 'Timestamp', '#timestamp' ],
]
;
const docs = [
{'_id': 1,'_source': {'uuid': 2,'#timestamp': 3}},
{'_id': 4,'_source': {'uuid': 5,'#timestamp': 6}},
];
const findInArr=function(value){
var t = ""
arr.forEach(item=>{
if (item.includes(value)) {t = item[0]}
})
return t
}
const description = dedent`
================================================
${
docs.map((element) => [
`${findInArr('_id')}: ${element['_id']}`,
`${findInArr('UUID')}: ${element['_source'].uuid}`,
`${findInArr('Timestamp')}: ${element['_source']['#timestamp']}\n`,
].join('\n')).join('--------------------\n')
}`;
console.log(description);

Sorting array of object based on length array inside of object

Suppose I have an array of object as,
const sampleArray = [ {name: 'Arthur',details: [ {"book":["harry pottar","lotr","something"]} ,{"book":["omega","beta","simba"]} ] },
{name: 'simmone',details: [ {"book":["ronjan","pirates"]} ,{"book":["musical","eskobar","elsa"]} ]} ]
I want output as based on total length of book, sort object.
O/P: [ {name: 'simmone',details: [ {"book":["ronjan","pirates"]} ,{"book":["musical","eskobar","elsa"]} ]},
{name: 'Arthur',details: [ {"book":["harry pottar","lotr"]} ,{"book":["omega","beta","simba"]} ]} ]
For this I calculated the total length of each book, as,
const bookDetails = sampleArray.map(i => i.book);
const emptyArray = bookDetails.map(
subarr => subarr.reduce((a, b) => a + b.book.length, 0)
);
It gives me the length of book as [6,5], on basis of book count how can I sort the object as:
O/P: [ {name: 'simmone',details: [ {"book":["ronjan","pirates"]} ,{"book":["musical","eskobar","elsa"]} ]},
{name: 'Arthur',details: [ {"book":["harry pottar","lotr"]} ,{"book":["omega","beta","simba"]} ]} ]
If anyone needs any further information, please let me know
You could use customized sort to achieve this result. But it is highly inefficient because it is calculating the length of the books each and every time when customizing algorithm place element before and after. Better optimized version added below.
const sampleArray = [{
name: "Arthur",
details: [{
book: ["harry pottar", "lotr", "something"]
},
{
book: ["omega", "beta", "simba"]
},
],
},
{
name: "simmone",
details: [{
book: ["ronjan", "pirates"]
},
{
book: ["musical", "eskobar", "elsa"]
},
],
},
];
function getBookLength(arr) {
return arr.reduce((acc, curr) => acc + curr.book.length, 0);
}
const result = sampleArray.sort((a, b) => {
return getBookLength(a.details) - getBookLength(b.details);
});
console.log(result);
FOR BETTER PERFORMANCE: It would be better to store the length of the array so that we don't have to calculate each and every time
const sampleArray = [
{
name: "Arthur",
details: [
{ book: ["harry pottar", "lotr", "something"] },
{ book: ["omega", "beta", "simba"] },
],
},
{
name: "simmone",
details: [
{ book: ["ronjan", "pirates"] },
{ book: ["musical", "eskobar", "elsa"] },
],
},
];
const lengthDict = sampleArray.reduce((acc, { name, details }) => {
const length = details.reduce((a, b) => a + b.book.length, 0);
acc[name] = length;
return acc;
}, {});
const result = sampleArray.sort((a, b) => {
return lengthDict[a.name] - lengthDict[b.name];
});
console.log(result);
If you can do that, then you got everything you want, but you need to understand Sorting algorithm. I see that you didn't specify the direction of the sorting, like less books first, or more book first. But suppose we need More Books first:
const sortedArray = array.sort((elem1, elem2) => {
// Return less than 0 to keep elem1 before elem2
// Return greater than 0 to put elem2 before elem1
// Return 0 to keep as is, regarding the rest of elements
if (elem1.length > elem2.length) {
return -1;
} else if (elem1.length < elem2.length) {
return 1;
}
return 0;
});
Apply your reduce algorithm and you're good. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
this way :
const sampleArray =
[ { name: 'Arthur', details:
[ { book: [ 'harry pottar', 'lotr', 'something'] }
, { book: [ 'omega', 'beta', 'simba'] }
] }
, { name: 'simmone', details:
[ { book: [ 'ronjan', 'pirates'] }
, { book: [ 'musical', 'eskobar', 'elsa'] }
] } ]
const booksCnt =({details})=>details.reduce((s,{book})=>s+book.length ,0)
sampleArray.sort((a,b)=>booksCnt(a)-booksCnt(b))
console.log( sampleArray )
.as-console-wrapper {max-height: 100%!important;top:0;}
With the decpk optimization :
const sampleArray =
[ { name: 'Arthur', details:
[ { book: [ 'harry pottar', 'lotr', 'something'] }
, { book: [ 'omega', 'beta', 'simba'] }
] }
, { name: 'simmone', details:
[ { book: [ 'ronjan', 'pirates'] }
, { book: [ 'musical', 'eskobar', 'elsa'] }
] } ]
const booksCnt = ({details})=>details.reduce((s,{book})=>s+book.length ,0)
const NamesLens = Object.fromEntries(sampleArray.map(el=>([el.name,booksCnt(el)])) )
sampleArray.sort((a,b)=>NamesLens[a.name]-NamesLens[b.name] )
console.log( sampleArray )
.as-console-wrapper {max-height: 100%!important;top:0;}

How can i compare two arrays of different length and keys on the basis of matching key values?

I have tried this, but it is giving the correct result
Array1
Array1: [
{
id: 2,
city: 'washington',
code: 0099,
room: 5,
...
},
{
...
},
...
]
Array 2
Array2: [
{
"id": 2,
"name": "john"
"number": 727625,
"etage": 5,
"status": 0,
...
},
{
...
},
...
]
My Code
let Result = [];
if (Array1 && Array1.length > 0 && Array2 && Array2.length > 0) {
Array1.forEach((arr1, index) => {
Array2.forEach((arr2, index) =>{
if (arr1.id === arr2.id && arr1.room === arr2.etage) {
Result.push(arr1)
}
})
})
}
console.log(Result)
What I want ?
I want items(objects) of Array1 by comparing both arrays, where
both have same id's && room from Array1's object equal to the etage
from Array2's object.
Please guide me, how can I do this in ES6 style in React js?
The main problem with nested loops is the unnecessary iteration of each element of the first array and multiple iterations of the second array.
This approach takes two loops, one for generating all keys from array2 and the other to filter array1.
You could take a Set for compound key of id and etage and filte the array for getting the items with same id and room.
const
getKey = (...values) => values.join('|'),
keys = new Set(array2.map(({ id, etage }) => getKey(id, etage))),
result = array1.filter(({ id, room }) => keys.has(getKey(id, room));
With condition
room > etage
and a Map.
const
ids = array2.reduce(
(m, { id, etage }) => m.set(id, Math.min(etage, m.get(id) || 0)),
new Map
),
result = array1.filter(({ id, room }) => room > ids.get(id));
I'd do something like this:
Array1= [
{
id: 2,
city: 'washington',
code: 0099,
room: 5,
}
];
Array2= [
{
"id": 2,
"name": "john",
"number": 727625,
"etage": 5,
},
];
const result = Array1.filter(a1 => Array2.find(a2 => a1.id == a2.id) && Array2.find(a2 => a1.room == a2.etage));
console.log(result);
That will return a filtered array by room, etage and id.
You can use filter and some ES6 methods:
const arr1 = [
{
id: 1,
room: 1
},
{
id: 2,
room: 5
},
{
id: 3,
room: 3
}
];
const arr2 = [
{
id: 0,
etage: 0
},
{
id: 2,
etage: 5
},
{
id: 3,
etage: 3
}
];
const getTheSame = (arr1, arr2) => {
return arr1.filter(o1 =>
arr2.some(o2 => o1.id === o2.id && o1.room === o2.etage)
);
};
console.log("Result: ", getTheSame(arr1, arr2));

How to concatenate object values with same id

I have an array:
let ar = [
{
uid:1,
flat_no: 1
},
{
uid:2,
flat_no: 2
},
{
uid:1,
flat_no:3
}
];
If uid are same then I want to remove duplicate uid and concatenate its flat_no. The output array should be like this:
[
{
uid:1,
flat_no: [1,3]
},
{
uid:2,
flat_no: 2
}
];
You can use a combination of Array.reduce and Array.find.
If you find an existing item in your accumulator array, just update it's flat_no property, otherwise push it to the accumulator array.
let arr = [
{
uid: 1,
flat_no: 1
},
{
uid: 2,
flat_no: 2
},
{
uid: 1,
flat_no: 3
}
]
arr = arr.reduce((arr, item) => {
const existing = arr.find(innerItem => innerItem.uid === item.uid)
if (existing) {
existing.flat_no = Array.isArray(existing.flat_no)
? existing.flat_no
: [existing.flat_no]
existing.flat_no.push(item.flat_no)
} else {
arr.push(item)
}
return arr
}, [])
console.log(arr)
You can iterate over your array and fill an object (used as a hashmap here).
Once done, you extract the values to get your result.
let hashResult = {}
ar.forEach(element => {
if (hashResult[element.uid] == undefined) {
hashResult[element.uid] = { uid: element.uid, flat_no: [] }
}
hashResult[element.uid].flat_no.push(element.flat_no)
})
let result = Object.values(hashResult)
console.log(new Date(), result)
You can do this in a concise way with a single Array.reduce and Object.values to match your desired output:
let data = [ { uid:1, flat_no: 1 }, { uid:2, flat_no: 2 }, { uid:1, flat_no:3 } ];
const result = data.reduce((r, {uid, flat_no}) => {
r[uid] ? r[uid].flat_no = [r[uid].flat_no, flat_no] : r[uid] = {uid, flat_no}
return r
}, {})
console.log(Object.values(result))
1)Reduce the initial array to an object which has uid as the key and the flat_no as the value.
2)Then run a map on the keys to convert it into an array of objects with uid and flat_no.
1) First Step Code
let ar = [{uid:1, flat_no: 1},{uid:2, flat_no: 2},{uid:1, flat_no:3}];
let outputObj = ar.reduce((outputObj,currObj,currIndex) => {
let {uid,flat_no} = currObj
if (outputObj[uid]) {
outputObj[uid].push(flat_no)
}
else {
outputObj[uid] = [flat_no]
}
return outputObj
},{})
2)
let finalOutput = Object.keys(outputObj).map(key =>
({uid:key,flat_no:outputObj[key]}))
console.log(finalOutput)

Categories

Resources