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

82
Visualizações
Counting arrays by distinct value

I have an array of arrays. Each contains a list of information along with an id as below:

let array = [
['value1', 'value2', 'value3', 'value4', 'value5', 'id_1'], 
['value1', 'value2', 'value3', 'value4', 'value5', 'id_1'],
['value7', 'value8', 'value9', 'value10', 'value11', 'id_3'],
['value20', 'value21', 'value22', 'value23', 'value24', 'id_4'],
['value20', 'value21', 'value22', 'value23', 'value24', 'id_5'],
];

I want to count the distinct instances of each array. When comparing two arrays, if all indexes are the same, including the id, then this means the array is a duplicate and only 1 instance should be counted.

If all indexes are the same, but the id is different, then this means that this is a distinct instance of the array and should be added to the count.

The information needs to be displayed in an object as below. I am struggling to visualise how to accomplish this, my current approach is to remove the ids from arrays, add these to the object and then figure out a way to count the unique instances - but unsure if this is the most effective method.

let obj = [
{
  array=['value1', 'value2', 'value3', 'value4', 'value5'],
  count=1
},
{
  array=['value7', 'value8', 'value9', 'value10', 'value11'],
  count=1
},
{
  array=['value20', 'value21', 'value22', 'value23', 'value24'],
  count=2
}
];
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use something like this.

I iterated over the array, joined all array values except the last one (id) and then used the joined string as a hash value for my map object and then checked if the id is is different then update the count else dont change

const arr = [
  ['value1', 'value2', 'value3', 'value4', 'value5', 'id_1'],
  ['value1', 'value2', 'value3', 'value4', 'value5', 'id_1'],
  ['value7', 'value8', 'value9', 'value10', 'value11', 'id_3'],
  ['value20', 'value21', 'value22', 'value23', 'value24', 'id_4'],
  ['value20', 'value21', 'value22', 'value23', 'value24', 'id_5'],
];

const map = {};

arr.forEach(x => {
  const key = x.slice(0, -1).join('-');
  const id = x.at(-1);
  if (!map[key]) {
    map[key] = {
      count: 1,
      id,
    };
  } else {
    if (id !== map[key].id)
      map[key].count++;
  }
});

console.log(map);

const obj = Object.entries(map).map(([k, v]) => ({
  array: k.split('-'),
  count: v.count
}))

console.log(obj);

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