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

169
Visualizações
Create a calculated array based on limits

OK time to pick the hive mind brain.

I am creating a dropdown (well 2 of them) and I need them to do the following Height from 4'0" to 7'0" Weight from 100lb to 400lb in 5lb incraments.

What would be the best/easiest way to create this array without having to manually create an array

It just needs to be as simple as

const heights = [
    { id: '', name: '' },
]

I just to not know how to best create it in as few Lines of code or manually creating the array

Same with height in 5lb increments

EDIT: SO people know WHY I am asking this - try doing a google search and enjoy the frustration.

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

0

For the weights, you can use the Array.fill function as seen in this answer.

// https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp

const range = (start, stop, step = 1) =>
  Array(Math.ceil((stop - start) / step) + 1).fill(start).map((x, y) => x + y * step)

const weights = range(100, 500, 5).map((x, index) => ({
  id: index,
  name: x
}))

console.log(weights)

// or with one line of code

const w = Array(Math.ceil((500 - 100) / 5) + 1).fill(100).map((x, index) => ({
    name: x + index * 5, id: index
}))
console.log(w)

For the heights, you can use a simple algorithm as a while loop with a condition for the increment

const start = {
  integer: 4,
  fractionnal: 0
}
const end = {
  integer: 7,
  fractionnal: 0
}

const heights = []

let index = 1
while (start.integer < end.integer || start.fractionnal <= end.fractionnal) {
  heights.push({
    id: index,
    name: `${start.integer}.${start.fractionnal}`
  })

  if (start.fractionnal < 11) start.fractionnal += 1
  else {
    start.integer += 1
    start.fractionnal = 0
  }
}

console.log(heights)

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