I have an array of objects that looks like this:
[
{
type: 'car',
choices: [
'audi',
'honda',
'bmw',
'ford'
],
},
{
type: 'drink',
choices: [
'soda',
'water',
'tea',
'coffee'
],
},
{
type: 'food',
choices: [
'chips',
'pizza',
'cookie',
'pasta'
],
}
]
Using lodash how to transform it into something that looks like this:
[
{
question: [
{
drink: "tea"
},
{
car: "bmw"
}
]
},
{
question: [
{
food: "cookie"
},
{
car: "ford"
}
]
},
{
question: [
{
drink: "soda"
},
{
food: "pizza"
}
]
},
{
question: [
{
food: "chips"
},
{
drink: "water"
}
]
},
{
question: [
{
car: "audi"
},
{
food: "pasta"
}
]
},
{
question: [
{
car: "honda"
},
{
drink: "coffee"
}
]
},
]
The logic is as follow:
I tried to Flatten the array using this function
let flattenItems = _.flatMap(items, ({ type, choices}) =>
_.map(choices, choice => ({
question: [
{ type: type, choice: choice },
{ type: type, choice: choice }
],
})
));
but it's not what I need, and it's not random. I not sure my approach is the correct one, I'm thinking I should use a filter or reduce
Any help on how to solve this would be appreciated using JS or lodash would be good.
If you want to merge or combine multiple arrays, string, numbers into a single array using lodash then you can use its _. concat() method. const str = 50; const arr1 = [20]; const arr2 = [30, 35]; const arr3 = [[10]] const final_arr = _.
The Lodash. flatten() method is used to flatten the array to one level deep. Parameter: This method accepts single parameter array that holds simple array or array of arrays. Return Value: The return type of this function is array.
Lodash's `merge()` Function Given two objects destination and source , Lodash's merge() function copies the 2nd object's own properties and inherited properties into the first object.
The _. sortBy() method creates an array of elements which is sorted in ascending order by the results of running each element in a collection through each iteratee. And also this method performs a stable sort which means it preserves the original sort order of equal elements.
You could get a combination from types
and a random choices
selecten with a check if a value is aleady used.
function getCombinations(array, size) {
function c(left, right) {
function getQuestion({ type, choices }) {
var random;
do {
random = choices[Math.floor(Math.random() * choices.length)];
} while (taken.get(type).has(random))
taken.get(type).add(random);
return { [type]: random };
}
left.forEach((v, i, a) => {
var temp = [...right, v];
if (temp.length === size) {
result.push({ question: temp.map(getQuestion) });
} else {
c([...a.slice(0, i), ...a.slice(i + 1)], temp);
}
});
}
var result = [],
taken = new Map(array.map(({ type }) => [type, new Set]));
c(array, []);
return result;
}
var data = [
{ type: 'car', choices: ['audi', 'honda', 'bmw', 'ford'] },
{ type: 'drink', choices: ['soda', 'water', 'tea', 'coffee'] },
{ type: 'food', choices: ['chips', 'pizza', 'cookie', 'pasta'] }
];
console.log(getCombinations(data, 2));
.as-console-wrapper { max-height: 100% !important; top: 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With