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

190
Views
Convierta dos matrices en un objeto, con la misma clave para cada matriz en Javascript

Por ejemplo, tengo dos matrices como estas.

 const dateArray = ['January', 'February', 'March', 'April', 'May', 'June'] const InfoArray = [ { Date : '2022-01', Active: 1 }, { Date : '2022-02', Active: 12 }, { Date : '2022-03', Active: 25 }, { Date : '2022-04', Active: 33 }, { Date : '2022-05', Active: 120 }, { Date : '2022-06', Active: 335 }, ]

Sin embargo, quiero combinar estas dos matrices en una matriz de objetos.

 const result = [ { month: 'January', Active: 1 }, { month: 'February', Active: 12 }, { month: 'March', Active: 25 }, { month: 'April', Active: 33 }, { month: 'May', Active: 120 }, { month: 'June', Active: 335 }, ]

Busqué información y me gustó

 const makeObjectWithTwoArray = () => { let chartObj = {} dateArray.forEach((element, index) => { chartObj[element] = infoArray[index] }) return chartObj }

sin embargo no funciona en absoluto. (por supuesto, porque cada matriz no puede tener las mismas claves que el código).

Su ayuda será muy apreciada.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar la función Array.prototype.map de la siguiente manera:

 const dateArray = ['January', 'February', 'March', 'April', 'May', 'June'] const InfoArray = [ { Date : '2022-01', Active: 1 }, { Date : '2022-02', Active: 12 }, { Date : '2022-03', Active: 25 }, { Date : '2022-04', Active: 33 }, { Date : '2022-05', Active: 120 }, { Date : '2022-06', Active: 335 }, ]; const result = InfoArray.map(({Date, Active}) => { const [_, month] = Date.split("-"); return {Active, month: dateArray[[Number(month) - 1]]}; }); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

 const dateArray = ['January', 'February', 'March', 'April', 'May', 'June'] const InfoArray = [ { Date : '2022-01', Active: 1 }, { Date : '2022-02', Active: 12 }, { Date : '2022-03', Active: 25 }, { Date : '2022-04', Active: 33 }, { Date : '2022-05', Active: 120 }, { Date : '2022-06', Active: 335 }, ] const result = InfoArray.map(item => { const monthNumber = item.Date.split('-')[1] const month = dateArray[+monthNumber - 1] return { Active: item.Active, month } }) console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

En realidad, no necesita el dateArray , solo puede extraer el nombre del mes de la propiedad Date .

 const InfoArray = [ { Date : '2022-01', Active: 1 }, { Date : '2022-02', Active: 12 }, { Date : '2022-03', Active: 25 }, { Date : '2022-04', Active: 33 }, { Date : '2022-05', Active: 120 }, { Date : '2022-06', Active: 335 }, ] let result = InfoArray.map(e => ({ month: new Date(e.Date).toLocaleString('default', {month: 'long'}), Active: e.Active })) console.log(result)

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!