Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda