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

189
Vistas
Javascript: Adding elements of multiple integer and integer ranges into an array without also adding the ranges themselves

GOAL: add all elements from an array of integers and arrays, each child array is a set of 2 integers denoting a start and end of a range.

HTML invocation:

    <div class="block" onclick="test([33,88,[1,5],[8,13],[22,25]]);">click</div>
    <div id="paper"></div>

I have the following JavaScript:

    <script>
    function test(clickArray) {
    let theArray = [];
        for (i = 0; i < clickArray.length; i++) {
            theArray.push(clickArray[i]);
            if (clickArray[i].length > 1) {
                range(clickArray[i][0], clickArray[i][1], theArray);
            }
        }
    writeArray(theArray);
    }
    
    function writeArray(anArray) {
    let pencilArray = [];
        for (let i = 0; i < anArray.length; i++) {
            pencilArray.push();
        }
            document.getElementById("paper").innerHTML = pencilArray.join("<br>");
    }
    
    function range(start, end, rangeArray) {
        for (let i = start; i <= end; i++) {
            rangeArray.push(i);
        }
        return rangeArray;
    }
    </script>

When I invoke the onclick JavaScript the output is:

33
88
1,5
1
2
3
4
5
8,13
8
9
10
11
12
13
22,25
22
23
24
25

Both the range passed and the elements constructed by the range() function are added to the final array, but I just want the elements, not the ranges, so output should be:

33
88
1
2
3
4
5
8
9
10
11
12
13
22
23
24
25
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could use a simple reduce() operation:

const test = (array) => array.reduce((a, [s, e]) => {
  while(s <= e) a.push(s++);
  return a;
}, []);

const result = test([[1,5],[8,13],[22,25]]);

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

It is generally not a good idea to use inline event handlers. Here's a snippet using Event Delegation, and a one line reducer (test) to create the ranges from a string containing range values (two values or a single range)

document.addEventListener(`click`, handle);

// reducer here
const test = array => array.reduce( (acc, [s, e]) => 
  acc.concat( [...Array(e + 1 - s)].map( (v, i) => i + s) ), [] );

function handle(evt) {
  if (evt.target.dataset.ranges) {
    // make sure a range contains two values. This allows single numbers
    const createRawRange = val => val.length < 2 ? [val, val] : val;
    // create ranges array from dataset, convert strings to Number
    const ranges = evt.target.dataset.ranges.split(";")
      .reduce( (acc, val) => 
        [...acc, createRawRange(val.split(`,`)).map(Number)], [] );
    // apply reducer (test) and print
    document.querySelector(`pre`).textContent = JSON.stringify(test(ranges));
  }
}
Add ranges <button data-ranges="88;1,5;8,13;22,25;33;43,47">
<!--                            ^ single value --> 
  88; 1-5; 8-13; 22-25; 33; 43-47
</button>
<pre></pre>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here's how you can handle numbers along with ranges.

const test = (arr) => {
  const allNumbers = arr.reduce((acc, el) => {
    if (Array.isArray(el)) {
      const [start, end] = el;
      for (let i = start; i <= end; i++) {
        acc.push(i)
      }
    } else if (!Number.isNaN(el)) {
      acc.push(el)
    }
    return acc
  }, [])

  document.getElementById('paper').innerHTML = allNumbers.join('<br />');
}
<div class="block" onclick="test([[1,5],[8,13],[22,25], 99]);">click</div>
<div id="paper"></div>

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