Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!