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

148
Vistas
How to merge an array into another array's object value

I'm having two set of data as below:

let loadedData = [
    [
      {
        y: 12,
        x: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
      },
      {
        x: 10,
        y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
      }
    ],
    [
      {
        x: 32,
        y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
      }
    ]
];

and

let seriesData = [
  {
    name: 'Graph',
    type: 'Column',
    data: []
  },
  {
    name: 'Graph',
    type: 'line',
    data: []
  }
];

I want to add loadedData's values inserted into seriesData's data property. The expected result should be like this.

result = [
  {
    name: 'Graph',
    type: 'Column',
    data: [
          {
            y: 12,
            x: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
          },
          {
            x: 10,
            y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
          }
        ],
  },
  {
    name: 'Graph',
    type: 'line',
    data: [
          {
            x: 32,
            y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
          }
        ]
  }
];

I looked around for solutions but I couldn't find one that suits my criteria. Here's what I tried.

let loadedData = [
  [{
      y: 12,
      x: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
    },
    {
      x: 10,
      y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
    }
  ],
  [{
    x: 32,
    y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
  }]
];

let seriesData = [{
    name: 'Graph',
    type: 'Column',
    data: [],
  },
  {
    name: 'Graph',
    type: 'line',
    data: []
  }
];


for (let i = 0; i < loadedData.length; i++) {
  const series = this.widget.config["options"].map((type) => {
    console.log(type.graphType);
    let el = [];
    this.loadedData.forEach(element => {
      el.push(element);
    });
    return {
      name: type.sensor.sensorType,
      type: type.graphType,
      data: el[i]
    };
  });
  seriesData = series;
}

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

0

If you don't need a copy of seriesData and loadedData then .forEach() is all you need

seriesData.forEach((serie, index) => {
  serie.data = loadedData[i] || serie.data;
});

let loadedData = [
    [ { x: 12, y: 'a' }, { x: 10, y: 'b' } ],
    [ { x: 32, y: 'c' } ]
];

let seriesData = [
  { name: 'Graph', type: 'Column', data: [] },
  { name: 'Graph', type: 'line', data: [] }
];

seriesData.forEach((serie, index) => {
  serie.data = loadedData[index] || serie.data;
});

console.log(seriesData);

If you need an copy:

const result = seriesData.map((serie, index) => {
  return {
    ...serie,
    data: (loadedData[index] || serie.data).map(d => Object.assign({}, d))
  };
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

Immutable ES6 solution using Array map:

seriesData.map((obj, index) => ({ ...obj, data: loadedData[index] }));

const loadedData = [
  [{
      y: 12,
      x: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
    },
    {
      x: 10,
      y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
    }
  ],
  [{
    x: 32,
    y: 'Tue Sep 14 2021 05:15:38 GMT-0700 (Pacific Daylight Time)'
  }]
];

const seriesData = [{
    name: 'Graph',
    type: 'Column',
    data: [],
  },
  {
    name: 'Graph',
    type: 'line',
    data: []
  }
];

const seriesFilledData = seriesData.map((obj, index) => ({ ...obj, data: loadedData[index] }));
console.log(seriesFilledData);

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