Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Cómo fusionar una matriz en el valor del objeto de otra matriz

Tengo dos conjuntos de datos como se muestra a continuación:

 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)' } ] ];

y

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

Quiero agregar los valores deloadedData insertados en la propiedad de datos de seriesData. El resultado esperado debería ser así.

 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)' } ] } ];

Busqué soluciones pero no pude encontrar una que se ajustara a mis criterios. Esto es lo que probé.

 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 answers
Answer question

0

Si no necesita una copia de seriesData loadedData , entonces .forEach() es todo lo que necesita

 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);

Si necesita una copia:

 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 Report

0

Solución ES6 inmutable usando el map de matriz:

 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!