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

185
Visualizações
Getting the first ( i.e. lowest) value from a 'range' of value e.g. £25 - £30

So I have here some JS code for getting either a singular value or, if there's a 'range' of prices separated by ' - ', getting the highest value:

function getPrice() {
  var str = ("£25 - £36");

  var val;
  if (str.indexOf('-') > -1) {
    val = Number(str.replace(/[^0-9.-]+/g, "").split('-')[1], 10);
  } else {
    val = Number(str.replace(/[^0-9.-]+/g, "").split('-')[0], 10);
  }
  return val;
}

How would I modify this so that it gets the lowest value instead?

This 'range' will always be either in the format of £25 or £13 - £72 and it will always be in pounds. The highest value will always be the last; in this context they are functionally the same.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

One way to achieve what you require would be to use a Regex to pull out all the figures in the string. You can then apply those matches to Math.min to find the lowest one.

The benefit of this approach is that it will retrieve the lowest number regardless of its position in the string.

function getLowestPrice(input) {
  let amounts = input.match(/\d{1,3}(,\d{3})*(\.\d+)?/g);
  amounts = amounts.map(v => v.replace(',', '')); // remove comma grouping
  return Math.min.apply(this, amounts);
}

console.log(getLowestPrice('£25 - £36')); // = 25
console.log(getLowestPrice('£1,090.99 - £77.25')); // = 77.25

Edit - I just expanded the Regex to handle values containing thousands grouping and decimals, credit to this answer for that.

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