Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
How to compare two list and find if element from first list exist between two value of the other list

I have list of ages and agesGroups

I want to take each element of the list ages and search in list ageGroup the interval where it is to recover the price ,If an element is in two interval so we recover the lowest price of the list ,I want the sum of the total prices.

For example :

for age 8 I have price =50 and for 20 and 67 i have two choices 90 and 70 ,I will choose the lowest price which is 70 and then the result have to be 70 + 70 + 50 = 190 price = 190

let ages = [8, 20, 67]
let agesGroups = [{
    min: 10,
    max: 90,
    price: 90
  },
  {
    min: 10,
    max: 90,
    price: 70
  },
  {
    min: 0,
    max: 10,
    price: 50
  }
]
let price = 0
ages.forEach(a => {
  agesGroups.forEach(s => {
    if (a >= s.min && a <= s.max) {
      price = price + parseInt(s.price, 10);
    }
  })
})

console.log(price);

The result that i got

price = 140

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Solution - by the suggestion of @Barmar - using reduce(). "result" now contains minimal prices for all the ages. You can summarize them if need:

let result = []
ages.forEach(a => {
    result.push(agesGroups.reduce((res, s) => {
        if(a >= s.min && a <= s.max) {
            if (s.price < res) { res = s.price}
        }
        return res
    }, Infinity));
});
console.log(result)
about 4 years ago · Juan Pablo Isaza Denunciar

0

Sometimes using Array methods are to restrictive of what's returned and it's difficult to navigate. A for...of loop is easier to read and refactor with Array methods later.

  • Define prices array to keep each best price

  • Outer for...of loop to iterate ages array

    • Define inRange array to keep each price within a range

    • Inner for...of loop to iterate ranges array

      • Determine if age is within range of range.min and range.max
      • Push range.price into inRange array if age is within range
    • Push the smallest range.price of inRange into prices array

      prices.push( Math.min(...inRange));
      
  • Then add all prices of price array

    prices.reduce((sum, add) => sum + add)
    

let ages=[8,20,67],ranges=[{min:10,max:90,price:90},{min:10,max:90,price:70},{min:0,max:10,price:50}];
let prices = [];

for (let age of ages) {
  let inRange = [];
  for (let range of ranges) {
    if (age >= range.min && age <= range.max) {
      inRange.push(range.price);
    }
  }
  prices.push( Math.min(...inRange));
}

let total = prices.reduce((sum, add) => sum + add);

console.log(total);
      

Refactored

let ages=[8,20,67],ranges=[{min:10,max:90,price:90},{min:10,max:90,price:70},{min:0,max:10,price:50}];

let total = ages.map(age => {
  let prices = ranges.flatMap(rng => rng.min <= age && rng.max >= age ? [rng.price] : []);
  return Math.min(...prices);
  }).reduce((sum, add) => sum + add);

console.log(total)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda