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

191
Visualizações
Generate brackets with steps of 10 from an array and eliminate empty steps

I'm having trouble solving this problem:

I have an array of objects:

tableList = [
{table_number: 1},
{table_number: 11},
{table_number: 31}
]

I'm trying to generate a bracket list in steps of 10 up to the largest 'table_number'.

I'm using a sequence generator function for this:

// Sequence generator function (commonly referred to as "range", e.g. Clojure, PHP etc)
const range = (start, stop, step) => Array.from({ length: (stop - start) / step + 1}, (_, i) => start + (i * step));

I'm using Math.max and iterate through the array to find the biggest 'table_number':

// Search for biggest table number
var max = Math.max.apply(Math, data.map(o => o.table_number));

I then generate the bracket array with the sequencing function:

// Generate brackets up until biggest table number
var brackets = range(0, max, 10);

This gives me a bracket array of [0, 10, 20, 30].

Now the question is, how do I evaluate the array of objects to remove the empty bracket step (20) that will not contain any 'table_number'?

Or should I be generating the bracket array differently?

I would really appreciate any help for this, thanks!

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

0

In case anyone stumbles on this, I have found this to be my solution.

const brackets = [...new Set(data.map(item => item.table_number - (item.table_number % 10)))]

I visit each element in the object and then subtract from it's remainder to get the numbers in steps of 10.

I then create a Set object from the outcome, effectively removing all duplicates.

about 4 years ago · Juan Pablo Isaza Relatório

0

Or should I be generating the bracket array differently?

If you're just after an array like [0,10,30] you can follow a different path:

tableList = [
{table_number: 1},
{table_number: 11},
{table_number: 31}
]


const tmpObj = tableList.reduce( (acc,cur) => {
  acc[ Math.floor(cur.table_number/10)*10 ] = true;
  return acc;
} , {})

const brackets = Object.keys(tmpObj).map((x) => parseInt(x));
console.log(brackets);

But this is taking advantage of how easy is "in steps of 10"

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