I have an array
let arr = [
{ type: 1, title: 1 },
{ type: 1, title: 1 },
{ type: 2, title: 1 },
{ type: 2, title: 1 },
{ type: 2, title: 1 },
{ type: 2, title: 1 },
{ type: 2, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 1, title: 1 },
{ type: 1, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 1, title: 1 },
{ type: 1, title: 1 },
{ type: 3, title: 1 },
{ type: 3, title: 1 },
{ type: 1, title: 1 },
{ type: 1, title: 1 }]
I want data = 1(type==1):2(type==2):1type==1(3) (5:10:5)
There may be a lot of data, but no matter how much data there is, I only hope to finally get 1:2:1 data I want last result, for example
last arr = [
{
type: 1,
child: [{ type: 1, title: 1 },{ type: 1, title: 1 },{ type: 1, title: 1 },{ type: 1, title: 1 },{ type: 1, title: 1 }]
},
{
type: 2,
child: [{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 },{ type: 2, title: 1 }]
},
{
type: 3,
child: [{ type: 3, title: 1 },{ type: 3, title: 1 },{ type: 3, title: 1 },{ type: 3, title: 1 },{ type: 3, title: 1 }]
}
]
CodePudding user response:
To get the desired output, in the desired ratio, we can loop, adding the correct ratio of items to the output, from the input array, until no more addition is possible, or we've reached the max count (in this case 20).
For example, if the ratio is 1:2:1, we add 1,2,1 items of each type for each iteration, stopping if
- a.) There are no items of a given type left.
- b.) We've reached our target count (in this case 20)
We create a function takeNItems()
which removes N items of a certain type from an array and returns in another array.
let arr = [ { type: 1, title: 1 }, { type: 1, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 1, title: 1 }, { type: 1, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 1, title: 1 }, { type: 1, title: 1 }, { type: 3, title: 1 }, { type: 3, title: 1 }, { type: 1, title: 1 }, { type: 1, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, { type: 2, title: 1 }, ]
// The ratios by type
const ratios = { 1: 1, 2: 2, 3: 1 };
const totalCount = 20;
function takeNItems(arr, type, count) {
let result = [];
do {
let index = arr.findIndex(item => item.type === type);
if (index === -1) return null;
result.push(...arr.splice(index, 1))
} while (result.length < count)
return result;
}
let count = 0;
let roundCount = Object.values(ratios).reduce((sum, n) => sum n,0);
let result = Object.keys(ratios).map(type => ({ type: type, count: 0, child: [] }));
do {
let nextRound = result.map(({type}, i) => takeNItems(arr, type, ratios[type]));
// Not enough items for another round...
if (nextRound.flat().length < roundCount) break;
nextRound.forEach((items, i) => {
result[i].count = items.length;
result[i].child.push(...items);
});
count = nextRound.flat().length;
} while (count < totalCount);
console.log('Result:', result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>