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

144
Vistas
Issue while looping 2D array javascript

I'm trying a truly easy thing which is looping inside a 2D array and creating another array by checking row per row.

For example, my 2D array contains the hex colors of a range of cells from a Google Sheet. I want to check every line and verify if on of the element id either red or orange.

Here is my sample code :

var 2Darray = [ [ '#ff0000', '#000000', '#000000' ],
              [ '#000000', '#ff6d01', '#000000' ],
              [ '#000000', '#000000', '#000000' ] ]
var redHex = "#ff0000";
var orangeHex = "#ff6d01";

var newColors = [];
for (var i = 0; i < rangeColor.length; i++) {
    var keepGoing  = true; 
    for (var j = 0; j < rangeColor[i].length; j++) {
        switch (rangeColor[i][j]) {
            case redHex:
                newColors[i] = redHex;
                console.log("1")
                keepGoing = false;
                break;
            case orangeHex:
                newColors[i] = orangeHex;
                console.log("2");
                keepGoing = false;
                break;
            case "#000000":
                newColors[i] = "#000000";
                console.log("3")
                keepGoing = false;
                break;
        }
        if (!keepGoing) {
          break;
        } 
    }
}    
console.log(newColors);

My new array should be : [ '#ff0000', '#ff6d01', '#000000' ] but is [ '#ff0000', '#000000', '#000000' ] which indicates that there is an issue with my loop but I can't figure out what. Maybe the break behavior with double for loop ?

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

0

Although I'm not sure whether I could correctly understand your script, if 2Darray is rangeColor, in your script, how about removing keepGoing = false; in case "#000000": as follows?

Because when the 2nd element of the array is checked, only the 1st element is checked because keepGoing = false; at case "#000000":. If 2Darray is rangeColor, I thought that this might be the reason for your issue.

And, in your script, I thought that var newColors = []; is required to be added.

When your script is modified, how about the following modification?

Modified script:

var newColors = [];
var rangeColor = [ [ '#ff0000', '#000000', '#000000' ],
              [ '#000000', '#ff6d01', '#000000' ],
              [ '#000000', '#000000', '#000000' ] ];
var redHex = "#ff0000";
var orangeHex = "#ff6d01";
for (var i = 0; i < rangeColor.length; i++) {
    var keepGoing  = true; 
    for (var j = 0; j < rangeColor[i].length; j++) {
        switch (rangeColor[i][j]) {
            case redHex:
                newColors[i] = redHex;
                console.log("1")
                keepGoing = false;
                break;
            case orangeHex:
                newColors[i] = orangeHex;
                console.log("2");
                keepGoing = false;
                break;
            case "#000000":
                newColors[i] = "#000000";
                console.log("3")
                // keepGoing = false; // Removed
                break;
        }
        if (!keepGoing) {
          break;
        } 
    }
}    
console.log(newColors);

As other method, how about the following sample script?

Sample script:

var rangeColor = [
  ['#ff0000', '#000000', '#000000'],
  ['#000000', '#ff6d01', '#000000'],
  ['#000000', '#000000', '#000000']
];
var redHex = "#ff0000";
var orangeHex = "#ff6d01";

var newColors = rangeColor.map(r => r.includes(redHex) ? redHex : r.includes(orangeHex) ? orangeHex : '#000000');
console.log(newColors) // ==> [ '#ff0000', '#ff6d01', '#000000' ]

References:

  • includes()
  • map()
about 4 years ago · Juan Pablo Isaza Denunciar

0

I am considering a typo in your question that 2Darray = rangeColor.

It is because, you are breaking your inner loop in the first iteration itself every time. And hence your answer is the first element of each row.

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