Estoy tratando de obtener los objetos que tienen un valor similar. Estoy intentando con esto pero no obtengo los objetos con valores similares:
const dataWork = [ { _id:1, firstName:'Leo', lastName:'Bla', mail:'leo.bla@gmail.com', certifications:[ {_id:1, certification:'C1 Certification'}, {_id:2, certification:'C3 Certification'} ], education:[ {_id:1, school:'School A'} ], work:[ {_id:1, company:'Company A', industry:'Industry A' ...}, {_id:2, company:'Company B', industry:'Industry B' ...}, {_id:3, company:'Company A', industry:'Industry A' ...}, {_id:4, company:'Company C', industry:'Industry C' ...}, {_id:5, company:'Company C', industry:'Industry C' ...}, {_id:6, company:'Company B', industry:'Industry A' ...}, {_id:7, company:'Company D', industry:'Industry B' ...}, ] } ];Quiero obtener esto:
result = [ { industryA:[ {_id:1, company:'Company A' ...}, {_id:3, company:'Company A' ...}, {_id:6, company:'Company A' ...} ], industryB:[ {_id:2, company:'Company B' ...}, {_id:7, company:'Company B' ...} ], industryC:[ {_id:4, company:'Company C' ...}, {_id:5, company:'Company C' ...} ] } ]¿cualquier sugerencia?
Pruebe el siguiente código
const data = { work:[ {_id:1, company:'Company A', industry:'Industry A'}, {_id:2, company:'Company B', industry:'Industry B'}, {_id:3, company:'Company A', industry:'Industry A'}, {_id:4, company:'Company C', industry:'Industry C'}, {_id:5, company:'Company C', industry:'Industry C'}, {_id:6, company:'Company B', industry:'Industry A'}, {_id:7, company:'Company D', industry:'Industry B'} ]} const obj = {}; data.work.map(el => { obj[el.industry] = obj[el.industry] ? [...obj[el.industry], el] : [el]; }) console.log(obj);