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

208
Views
¿Cómo convertir un objeto anidado Array en un objeto Array?

tengo una matriz:

 let testData = [ { MC: "11233", jobid: 113331, list: [ { Q1: 1113, Q2: 333, code: "thisis1" }, { Q1: 333, Q2: 111, code: "thisis2" }, { Q1: 333, code: "thisis3" }, ], }, { MC: "332211", jobid: 3333, list: [ { Q1: 444, Q2: 555, code: "thisis4" }, ], }, ];

Y quiero convertirlo en:

 [ { MC: "11233", jobid: 113331, Q1: 1113, Q2: 333, code: "thisis1" }, { MC: "11233", jobid: 113331, Q1: 333, Q2: 111, code: "thisis2" }, { MC: "11233", jobid: 113331, Q1: 333, code: "thisis3" }, { MC: "332211", jobid: 3333, Q1: 444, Q2: 555, code: "thisis4" }, ]

Traté de escribir una función recursiva como esta

 let newData = []; testData.forEach((element, index) => { let flattedObj = {}; deconstructeArrayObject(flattedObj, element); });

y

 const deconstructeArrayObject = (flattedObj, targetObj) => { let tempArr = []; for (let key in targetObj) { if (Array.isArray(targetObj[key])) { targetObj[key].forEach((element) => { tempArr.push( ...JSON.parse( JSON.stringify( deconstructeArrayObject(flattedObj, element, outputArray) ) ) ); }); } else { flattedObj[key] = targetObj[key]; } } return flattedObj; // actually I want this function to return an array };

No tengo idea de cómo debo alcanzar mi meta. ¿Hay alguna pista o sugerencia?

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Itere la matriz externa, desestructurando sus propiedades, luego haga lo mismo para cada matriz interna, insertando los datos que desea en su resultado:

 const input = [ { MC: "11233", jobid: 113331, list: [ { Q1: 1113, Q2: 333, code: "thisis1" }, { Q1: 333, Q2: 111, code: "thisis2" }, { Q1: 333, code: "thisis3" }, ], }, { MC: "332211", jobid: 3333, list: [ { Q1: 444, Q2: 555, code: "thisis4" }, ], }, ]; const result = []; for (const {MC, jobid, list} of input) { for (const {Q1, Q2, code} of list) { result.push(({MC, jobid, Q1, Q2, code})); } } console.log(result);

about 4 years ago · Santiago Trujillo Report

0

Qué tal esto:

 let testData = [{"MC":"11233","jobid":113331,"list":[{"Q1":1113, "test": [{ 'a' : 1}, { 'a' : 2}],"Q2":333,"code":"thisis1"},{"Q1":333,"Q2":111,"code":"thisis2"}, {"Q1":333,"code":"thisis3"}]} ,{"MC":"332211","jobid":3333,"list":[{"Q1":444,"Q2":555,"code":"thisis4"}]}] const deconstructeArrayObject = (targetObj) => { let flattedObj = {}; let tempArr = []; let arrayKeys = []; for (let key in targetObj) { // Save array keys if (Array.isArray(targetObj[key])) { arrayKeys.push(key); } else { flattedObj[key] = targetObj[key]; } } // flatten array elements arrayKeys.forEach(key => { targetObj[key].forEach((element) => { const newEl = { ...flattedObj, ...element }; // Recursion const arr = deconstructeArrayObject(newEl); tempArr.push(...arr); }); }); if(tempArr.length === 0) { return [flattedObj]; } return tempArr; } let newData = []; testData.forEach((element, index) => { const result = deconstructeArrayObject(element); newData.push(...result) }); console.log(newData)

El resultado será:

 [ { "MC": "11233", "jobid": 113331, "Q1": 1113, "Q2": 333, "code": "thisis1", "a": 1 }, { "MC": "11233", "jobid": 113331, "Q1": 1113, "Q2": 333, "code": "thisis1", "a": 2 }, { "MC": "11233", "jobid": 113331, "Q1": 333, "Q2": 111, "code": "thisis2" }, { "MC": "11233", "jobid": 113331, "Q1": 333, "code": "thisis3" }, { "MC": "332211", "jobid": 3333, "Q1": 444, "Q2": 555, "code": "thisis4" } ]
about 4 years ago · Santiago Trujillo Report

0

Aquí hay una solución más general que no depende de los nombres de clave específicos, etc.

Lo que te faltaba era la respuesta al subproblema: how do I merge multiple "deconstructed" objects in an array into my "flattedObj"?

Esto se hace con la llamada a Object.assign en la función a continuación.

 function deconstructeArrayObject(obj) { const deconstructedObj = {}; for (const key in obj) { if (Array.isArray(obj[key])) { Object.assign( deconstructedObj, ...obj[key].map(deconstructeArrayObject) ); } else { deconstructedObj[key] = obj[key]; } } return deconstructedObj; } // use like this let newData = testData.map(deconstructeArrayObject);
about 4 years ago · Santiago Trujillo 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!