I am trying to get the objects that have the similar value. I am trying with this but I am not getting the objects with similar values:
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' ...},
]
}
];
I want obtain this:
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' ...}
]
}
]
any suggestion?
Try below code
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);