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 }, ]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 });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; }