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

172
Views
How to allocate data proportionally

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 }]
    }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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; }

about 4 years ago · Juan Pablo Isaza 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!