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

138
Visualizações
Calcular cuántos artículos dentro de un rango

Estoy tratando de reunir algunos datos basados en algunos datos.

Entonces, por ejemplo, como se ve a continuación, la matriz de items muestra la cantidad total de veces que un número está entre el rango de los datos, por ejemplo, 4500 - 5000 tiene 4 números entre ese rango.

Sin embargo, sumarlos no da una representación precisa de la cantidad de artículos que hay. Si esto tiene sentido? Como en el result , el recuento de 7000 es 13, sin embargo, debería ser 8.

 const data = [ 2200, 3900, 4500, 4500, 4800, 5000, 6000, 6000 ] const items = [ { value: { from: 2000, to: 2500 }, count: null }, { value: { from: 2500, to: 3000 }, count: null }, { value: { from: 3000, to: 3500 }, count: null }, { value: { from: 3500, to: 4000 }, count: null }, { value: { from: 4000, to: 4500 }, count: null }, { value: { from: 4500, to: 5000 }, count: null }, { value: { from: 5000, to: 5500 }, count: null }, { value: { from: 5500, to: 6000 }, count: null }, { value: { from: 6000, to: 6500 }, count: null }, { value: { from: 6500, to: 7000 }, count: null }, ].map(item => { item.count = data.filter(x => x <= item.value.to && x >= item.value.from).length; return item; }); let counter = 0; result = items.map(i => { counter += i.count; return { value: i.value.to, label: i.value.to, count: counter } }).filter(Boolean); console.log({items, result});

Lo que DEBO tener es lo siguiente, ya que estos datos son para un menú desplegable UPTO .

 [ { value: { from: 2000, to: 2500 }, count: 1 }, { value: { from: 2500, to: 3000 }, count: 1 }, { value: { from: 3000, to: 3500 }, count: 1 }, { value: { from: 3500, to: 4000 }, count: 2 }, { value: { from: 4000, to: 4500 }, count: 4 }, { value: { from: 4500, to: 5000 }, count: 6 }, { value: { from: 5000, to: 5500 }, count: 6 }, { value: { from: 5500, to: 6000 }, count: 8 }, { value: { from: 6000, to: 6500 }, count: 8 }, { value: { from: 6500, to: 7000 }, count: 8 }, ]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

No debe considerar tanto from como to dentro del límite. Esto dará como resultado tener elementos comunes en los grupos individuales.

Por ejemplo: De 4000 a 4500 significa números mayores a 4000 y menores o iguales a 4500.

Aquí, en el ejemplo anterior, el límite de los amantes no se superpone, por lo que los grupos individuales no tienen elementos comunes.

 const data = [2200, 3900, 4500, 4500, 4800, 5000, 6000, 6000] const items = [ { value: { from: 2000, to: 2500 }, count: null }, { value: { from: 2500, to: 3000 }, count: null }, { value: { from: 3000, to: 3500 }, count: null }, { value: { from: 3500, to: 4000 }, count: null }, { value: { from: 4000, to: 4500 }, count: null }, { value: { from: 4500, to: 5000 }, count: null }, { value: { from: 5000, to: 5500 }, count: null }, { value: { from: 5500, to: 6000 }, count: null }, { value: { from: 6000, to: 6500 }, count: null }, { value: { from: 6500, to: 7000 }, count: null }, ].map(item => { item.count = data.filter(x => x <= item.value.to && x > item.value.from).length; return item; }); let counter = 0; result = items.map(i => { counter += i.count; return { value: i.value.to, label: i.value.to, count: counter } }).filter(Boolean); console.log({ items, result });

about 4 years ago · Juan Pablo Isaza Relatório

0

Podría tomar un solo bucle y mapear un nuevo objeto con conteo.

 const data = [2200, 3900, 4500, 4500, 4800, 5000, 6000, 6000], values = [{ value: { from: 2000, to: 2500 }, count: null }, { value: { from: 2500, to: 3000 }, count: null }, { value: { from: 3000, to: 3500 }, count: null }, { value: { from: 3500, to: 4000 }, count: null }, { value: { from: 4000, to: 4500 }, count: null }, { value: { from: 4500, to: 5000 }, count: null }, { value: { from: 5000, to: 5500 }, count: null }, { value: { from: 5500, to: 6000 }, count: null }, { value: { from: 6000, to: 6500 }, count: null }, { value: { from: 6500, to: 7000 }, count: null }], items = values.map(item => ({ ...item, count: data.reduce((s, x) => s + (x <= item.value.to), 0) })); console.log(items);
 .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