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

144
Vistas
Count occurences of an object in a row before hitting a certain key in Javascript

I have an array of arrays that looks like this:

    var grid = [
        [0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0],
        [0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0]
    ];

I want to count the number of 1s in a row until we hit a zero. And then if we hit a 1 again, count the sequential occurrences again.

So for grid[0], it should return 9.
For grid[1], it should return [3,3].
grid[2], [2,1,1,2].

As an extra layer of fun, I'm also trying to do this for "columns." I.e. How many times "1" appears in the 1st column, which would consist of grid[0][1], grid[1][1], grid[2][1] and so on. If there's a better way to organize the data to achieve this, I'm open to suggestions as in total I have 25 rows/arrays within the array.

I'm unsure if there is a way that doesn't involve looping through the data over and over. Currently I'm doing this:

    var guides = [];

    for ( var i = 0; i < grid.length; i++ ) {
      var row = grid[i];
      var chunks = [];
      var count = 0;
    
      for (var j = 0; j < row.length; j++ ) {
         if ( j === 1 ) {
             count++;
         } else {
           chunks.push(count);
           count = 0;
         }
      }

    guides.push(chunks);

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

0

Here's one example how you can use recursion to check those number 1 strikes. For the columns, you can transpose the original Grid and re-use the same function.

var grid = [
  [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
]

// Returns the array of 1 strikes in the given arr
const occurrenceOfOne = (arr, strike = 0, final = []) => {
  // Recursive calls until the array is empty
  if (arr.length === 0) {
    return strike === 0 ? final : final.concat([strike])
  }

  // If the item is 0, the strike ends.
  if (arr[0] === 0) {
    if (strike !== 0) {
      return occurrenceOfOne(arr.slice(1), 0, final.concat([strike]))
    }
  }

  // If the item is 1, the strike continues
  if (arr[0] === 1) {
    return occurrenceOfOne(arr.slice(1), strike + 1, final)
  }

  // Default value 0 found and strike is 0 as well.
  return occurrenceOfOne(arr.slice(1), 0, final)
}

// Copied transpose function from this gist:
// https://gist.github.com/femto113/1784503
function transpose(a) {
  return a[0].map((_, c) => a.map((r) => r[c]))
}

console.log(occurrenceOfOne(grid[0]))
console.log(occurrenceOfOne(grid[1]))
console.log(occurrenceOfOne(grid[2]))

const transposedGrid = transpose(grid)
console.log(occurrenceOfOne(transposedGrid[6]))
console.log(occurrenceOfOne(transposedGrid[7]))

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