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

111
Vistas
Map both elements from 2d Array Javascript at the same time

My data array is a 2d array and each inner array holds data in the form [date, color]. I am trying to graph the dates and the colors and to do this I am trying to map the elements in the inner array. However, my current map function does not work when I try mapping two elements at once. How can I fix this?

shapes: data.map((date, type_color) => ({
                      
                        x0: date,
                        y0: 0,
                        x1: date,
                        y1: 1,
                        opacity: 0.8,
                        line: {
                            color: type_color,
                            width: 3,
                            dash: 'dot'
                        }
                    }))
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I think you mean to be using destructuring, but you aren't; you're just accepting two separate parameters in your map callback. When you do that, the first will be the [date, color] array (each element from the data array); the second will be the index of the element in data.

To use parameter destructuring, you'd use [date, type_color] in the parameter list:

shapes: data.map(([date, type_color]) => ({
//                ^                ^
    x0: date,
    y0: 0,
    x1: date,
    y1: 1,
    opacity: 0.8,
    line: {
        color: type_color,
        width: 3,
        dash: 'dot'
    }
}))

That applies [date, type_color] destructuring to the first parameter.

const data = [
    ["date1", "color1"],
    ["date2", "color2"],
    ["date3", "color3"],
];

const result = {
    shapes: data.map(([date, type_color]) => ({
    //                ^                ^
        x0: date,
        y0: 0,
        x1: date,
        y1: 1,
        opacity: 0.8,
        line: {
            color: type_color,
            width: 3,
            dash: 'dot'
        }
    }))
};
console.log(result.shapes);
.as-console-wrapper {
    max-height: 100% !important;
}

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