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

253
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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