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

201
Views
Cómo convertir un objeto en una matriz de múltiples objetos en javascript

Tengo un objeto de datos y quiero dividirlo en una matriz de objetos.

 let data = { "education_center-266": "Software House x", "education_center-267": "Learning Academy xyz", "end_date-266": "2022-01-26", "end_date-267": "2021-01-22", "start_date-266": "2021-01-26", "start_date-267": "1998-11-26", "title-266": "Web Developer", "title-267": "Teacher", }

Probé de diferentes maneras pero no pude alcanzar el resultado que quiero... el resultado debería ser

 [ { id: "266", education_center: "Software House x", title: "Web Developer", start_date: "2021-01-26", end_date: "2022-01-26", }, { id: "267", education_center: "Learning Academy xyz", title: "Teacher", start_date: "1998-11-26", end_date: "2021-01-22", }, ]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const myObjects = {}; Object.keys(data).map((key) => { const splitKey = key.split('-'); const elemId = splitKey[1]; const realKey = splitKey[0]; if (!myObjects[ elemId ]) { myObjects[ elemId ] = { id: elemId }; // Create entry } myObjects[ elemId ][ realKey ] = data[ key ]; }); // Turn into array const myObjectsToArray = Object.values(myObjects); // Or use the myObjects as a key/value store with ID as index const selectedElement = myObjects[ myID ];
about 4 years ago · Juan Pablo Isaza Report

0

const data = { "education_center-266": "Software House x", "education_center-267": "Learning Academy xyz", "end_date-266": "2022-01-26", "end_date-267": "2021-01-22", "start_date-266": "2021-01-26", "start_date-267": "1998-11-26", "title-266": "Web Developer", "title-267": "Teacher" }; const results = []; function splitKey(key) { const indexOfDelimiter = key.lastIndexOf("-"); return { id: key.substring(indexOfDelimiter + 1), key: key.substring(0, indexOfDelimiter) }; } function getItemFromResults(id) { return results.find((r) => r.id === id); } function processKeyValuePair(id, key, value) { const item = getItemFromResults(id); if (!item) { results.push({ id, [key]: value }); return; } item[key] = value; } for (const k in data) { const { id, key } = splitKey(k); const value = data[k]; processKeyValuePair(id, key, value); }
about 4 years ago · Juan Pablo Isaza Report

0

Aquí una solución ligeramente diferente, primero obteniendo todas las id's únicas , luego construyendo la matriz de result con un bucle sobre Object.entries .

 let data = { "education_center-266": "Software House x", "education_center-267": "Learning Academy xyz", "end_date-266": "2022-01-26", "end_date-267": "2021-01-22", "start_date-266": "2021-01-26", "start_date-267": "1998-11-26", "title-266": "Web Developer", "title-267": "Teacher", } const ids= [...new Set(Object.keys(data).map((el)=>{ return el.split('-')[1] }))]; console.log(ids) //unique id's let result = []; ids.forEach((i)=>{ let obj={id: i} Object.entries(data).forEach((el)=>{ if(el[0].includes(i)) obj[el[0].split('-')[0]]=el[1]; }) result.push(obj) }) 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!