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

182
Vistas
Count Value of multi dimensional array in Javascript
var sortedArr = [["Paul",     "1355"],
  ["Jennifer", "1910"],
  ["John",      "835"],
  ["John",      "830"],
  ["Paul",     "1315"],
  ["John",     "1615"],
  ["John",     "1640"],
  ["Paul",     "1405"],
  ["John",      "855"],
  ["John",      "930"],
  ["John",      "915"],
  ["John",      "730"],
  ["John",      "940"],
  ["Jennifer", "1335"],
  ["Jennifer",  "730"],
  ["John",     "1630"],
  ["Jennifer",    "5"]
];

I am trying to find the total number of counts for each key, For example, Paul appears three times so Paul should count 3 times.

and then want to write all the values for it. For example for Paul value should be 1355 1315 1405

It should be Like Paul 1355, 1315, 1405

I try something, but I get some issues, the issue are more about logic. Can someone please guide me?


  count = 1;
 var test;

//sortedArr = arr.sort();

for (var i = 0; i < sortedArr.length; i = i + count) {

  count = 1;
  test = [];
  test.push(sortedArr[i][1]);
 
  for (var j = i + 1; j < sortedArr.length; j++) {
  
    if (sortedArr[i][0] === sortedArr[j][0])
    {
      count++;
     // 
       test.push(sortedArr[j][1]);
       
  }
  }
  document.write(sortedArr[i][0]  +test +" = " + count + "<br>");
 
}

Output (wrong)

Paul1355,1315,1405 = 3
John830,1615,1640,855,930,915,730,940,1630 = 9
John940,1630 = 2
Jennifer730,5 = 2
Jennifer5 = 1

Output should be

Paul1355,1315,1405 = 3
John835, 830,1615,1640,855,930,915,730,940,1630 = 10
Jennifer1910, 1335, 730,5 = 4

Can someone tell me the issue in my code?

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

0

Your sortedArr isn't sorted, so the results are erratic. In JavaScript, sort() modifies the array directly, so you only need to add

sortedArr.sort();

and your code works.

var sortedArr = [["Paul",     "1355"],
  ["Jennifer", "1910"],
  ["John",      "835"],
  ["John",      "830"],
  ["Paul",     "1315"],
  ["John",     "1615"],
  ["John",     "1640"],
  ["Paul",     "1405"],
  ["John",      "855"],
  ["John",      "930"],
  ["John",      "915"],
  ["John",      "730"],
  ["John",      "940"],
  ["Jennifer", "1335"],
  ["Jennifer",  "730"],
  ["John",     "1630"],
  ["Jennifer",    "5"]
];

 count = 1;
 var test;

sortedArr.sort();

for (var i = 0; i < sortedArr.length; i = i + count) {
  count = 1;
  test = [];
  test.push(sortedArr[i][1]);
 
  for (var j = i + 1; j < sortedArr.length; j++) {    
    if (sortedArr[i][0] === sortedArr[j][0]) {
      count++; 
      test.push(sortedArr[j][1]);     
      }
  }
  document.write(sortedArr[i][0]  +test +" = " + count + "<br>");
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can first organize the data and then print it to the document.

  1. First you can iterate over the array using Array.prototype.reduce and convert it into object where each field will be <perosn_name> : <array_of_values>.

  2. Then again iterate to print in the form of <name> = <comma separated values> = <count> of the values (using Object.entries and Array.prototype.forEach)

Try like below

var sortedArr = [
  ["Paul", "1355"],
  ["Jennifer", "1910"],
  ["John", "835"],
  ["John", "830"],
  ["Paul", "1315"],
  ["John", "1615"],
  ["John", "1640"],
  ["Paul", "1405"],
  ["John", "855"],
  ["John", "930"],
  ["John", "915"],
  ["John", "730"],
  ["John", "940"],
  ["Jennifer", "1335"],
  ["Jennifer", "730"],
  ["John", "1630"],
  ["Jennifer", "5"],
];

// convert the data into the form described
const output = sortedArr.reduce((prevValue, [name, value]) => {
  prevValue[name] ? prevValue[name].push(value) : (prevValue[name] = [value]);
  return prevValue;
}, {});

// write to the document
Object.entries(output).forEach(([name, arr]) => {
  document.write(`${name} = ${arr.join(",")} = ${arr.length}<br>`);
})

NOTE: Always try to use the built-in Array prototype functions. Most of the time you can use them and easy to read your code.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Respecting and understanding the way you are trying to solve the problem, your logic is correct except that it needs a sorted list because your implementation skips consecutive entries with similar names with count it has to be sorted for that. So sorting the array before you check the values will give you the proper results:

sortedArr.sort((a, b) => b.toString().localeCompare(a.toString()));

Note I have sorted the array in descending order to match your output. If you just call sortedArr.sort() it should also give the correct results but in reverse output order.

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