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