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

246
Views
How to get number of occurrences in an array in array? Javascript

In JavaScript, I want to count the number of times "N" in is in the first, second, third, fourth column. I want each other there values. I want to get the number of occurrences in an array in array, and then get four numbers equal the occurrences.

input:

var set =[
['N', 'N', 'Y', 'N'],
['1', 'N', '2', 'N'],
['N', '1', '4', 'N'],
['2', 'N', 'N', '1']]

output: 3 2 2 2

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const set = [
  ['N', 'N', 'Y', 'N'],
  ['1', 'N', '2', 'N'],
  ['N', '1', '4', 'N'],
  ['2', 'N', 'N', '1'],
];
const countNs = row => row.reduce((acc, curr) => acc + (curr === 'N' ? 1 : 0), 0);
// number of Ns in each row
console.log(set.map(countNs));
const transpose = a => a[0].map((_, c) => a.map(r => r[c]));
// Number of Ns in each column
console.log(transpose(set).map(countNs));

about 4 years ago · Juan Pablo Isaza Report

0

The total count for the column-occurrence of a given value from a table needs to follow an approach similar to transposing a table into a matrix.

An generic implementation based on two nested reduce tasks then might look like the next provided code ...

// function transpose(result, row) {
//   return row.reduce((matrix, value, idx) => {
// 
//     (matrix[idx] ??= []).push(value);
//     return matrix;
// 
//   }, result);
// }

function aggregateColumnValueCount(collector, row) {
  // return row.reduce(({ value, counts = {} }, currentValue, idx) => {
  return row.reduce(({ value, counts = [] }, currentValue, currentColumn) => {

    // const currentColumn = `column_${ idx + 1 }`;
    const currentCount = (counts[currentColumn] ??= 0);

    if (currentValue === value) {
      counts[currentColumn] = currentCount + 1;
    }
    return { value, counts };

  }, collector);
}

const table = [
  ['N', 'N', 'Y', 'N'],
  ['1', 'N', '2', 'N'],
  ['N', '1', '4', 'N'],
  ['2', 'N', 'N', '1'],
];
console.log(
  "column counts for value 'N' ...",
  table
    .reduce(aggregateColumnValueCount, {

      value: 'N',
      counts: [],
      // counts: {},

    }).counts
);
console.log(
  // table.reduce(aggregateColumnValueCount, { value: '2', counts: {} })
  table.reduce(aggregateColumnValueCount, { value: '2', counts: [] })
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

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!