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

187
Views
Convierta un objeto en una matriz de objetos en base a una subcadena en las claves

Tengo un objeto que se parece a esto:

 const object = { User 1 Fecha de Nacimiento: "02/05/2000", User 1 Porcentage: 25, User 1 Primer Apellido: "Gonzalez", User 1 Segundo Apellido: "Perez", User 1 Sexo: "H", User 1 nombre: "Manuel", User 2 Fecha de Nacimiento: "02/05/2000", User 2 Porcentage: 25, User 2 Primer Apellido: "Diaz", User 2 Segundo Apellido: "Rodriguez", User 2 Sexo: "M", User 2 nombre: "Pepa", }

Me gustaría manipular este objeto para que se transforme en una matriz que se parece a esta base en la información de cada usuario (es decir, 1 o 2):

 const arrayOfObjects = [ { Fecha de Nacimiento: "02/05/2000", Porcentage: 25, Primer Apellido: "Gonzalez", Segundo Apellido: "Perez", Sexo: "H", Nombre: "Manuel" }, { Fecha de Nacimiento: "02/05/2000", Porcentage: 25, Primer Apellido: "Diaz", Segundo Apellido: "Rodriguez", Sexo: "M", Nombre: "Pepa" } ];

Necesitaré tener una función de ayuda para hacerlo, de modo que pueda almacenarlo en un estado de reacción (componente funcional)

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

0

 const srcObject = { "User 1 Fecha de Nacimiento": "02/05/2000", "User 1 Porcentage": 25, "User 1 Primer Apellido": "Gonzalez", "User 1 Segundo Apellido": "Perez", "User 1 Sexo": "H", "User 1 nombre": "Manuel", "User 2 Fecha de Nacimiento": "02/05/2000", "User 2 Porcentage": 25, "User 2 Primer Apellido": "Diaz", "User 2 Segundo Apellido": "Rodriguez", "User 2 Sexo": "M", "User 2 nombre": "Pepa", } const transformData = (src) => { const results = []; Object.keys(src).forEach(key => { const split = key.split(/^User (\d+) /).filter(i => i !== '') const index = +split[0] - 1 const newKey = split[1] if (!results[+index]) { results[+index] = {} } results[+index][newKey] = src[key] }) return results } console.log(transformData(srcObject))

about 4 years ago · Juan Pablo Isaza Report

0

El enfoque

  1. Organice la estructura del objeto dividiendo las claves del objeto (porque el orden difiere
  2. Agruparlos por parte de clave común
  3. Ahora recopile en una matriz

Implementación

 var obj = { "User 1 Fecha de Nacimiento": "02/05/2000", "User 1 Porcentage": 25, "User 1 Primer Apellido": "Gonzalez", "User 1 Segundo Apellido": "Perez", "User 1 Sexo": "H", "User 1 nombre": "Manuel", "User 2 Fecha de Nacimiento": "02/05/2000", "User 2 Porcentage": 25, "User 2 Primer Apellido": "Diaz", "User 2 Segundo Apellido": "Rodriguez", "User 2 Sexo": "M", "User 2 nombre": "Pepa", } const getPrefixAndSuffix = (str = "", value) => { //console.log("::", str) var index = str.indexOf(' ', str.indexOf(' ') + 1); var firstChunk = str.substr(0, index); var secondChunk = str.substr(index + 1); return { prefix: firstChunk, suffix: secondChunk, value: value } } function groupBy(list, keyGetter) { const map = new Map() list.forEach(item => { const key = keyGetter(item) const collection = map.get(key) if (!collection) { map.set(key, [item]) } else { collection.push(item) } }) return map } const getAsList = (groups = {}) => { const valuesArr = [] groups.forEach((value, key, map) => { var resultObj = {} value.forEach(value => { resultObj[value.suffix] = value.value }) valuesArr.push(resultObj) }); return valuesArr } const getFormatted = (obj) => { const asSplitList = Object.keys(obj).map((key) => { return getPrefixAndSuffix(key, obj[key]) }) const groupedItems = groupBy(asSplitList, (it) => it.prefix) return groupedItems } var format = getFormatted(obj) const finalList = getAsList(format) console.log(finalList)

about 4 years ago · Juan Pablo Isaza Report

0

 const object = { "User 1 Fecha de Nacimiento": "02/05/2000", "User 1 Porcentage": 25, "User 1 Primer Apellido": "Gonzalez", "User 1 Segundo Apellido": "Perez", "User 1 Sexo": "H", "User 1 nombre": "Manuel", "User 2 Fecha de Nacimiento": "02/05/2000", "User 2 Porcentage": 25, "User 2 Primer Apellido": "Diaz", "User 2 Segundo Apellido": "Rodriguez", "User 2 Sexo": "M", "User 2 nombre": "Pepa", } var obj = {}; for (const [key, value] of Object.entries(object)) { let splitedData = key.match(/User\s\d/); if (splitedData) { let user = splitedData[0]; if (!obj[user]) obj[user] = {}; let prop = key.replace(/User\s\d/i, '').trim(); obj[user][prop] = value; } } let outPut = Object.values(obj); console.log(outPut);

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!