node js typescript api create an object in specific pattern from array of object, How can i retrive data using for loop in typescript?
I'm facing some issue in for loop while creating an object from array of object.I have an object as this in node js app:
I am working for rest api have to get the response properly.
[
{
"issuer_id": 2639,
"job_title": "Sales Manager",
"hr-contact": "9767865459",
"adress": "bangalore",
"image": "http://localhost:3003/public/uploads/company-logos/undefined",
"getEmployerDetail": [
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 111,
"Section": "Communes",
"Content": "Mühlwald",
"foa_section_content_id": 111
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 112,
"Section": "Communes",
"Content": "Wolkenstein in Gröden",
"foa_section_content_id": 112
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 113,
"Section": "Communes",
"Content": "Schnals",
"foa_section_content_id": 113
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 150,
"Section": "Professional field",
"Content": "Marketing, Graphics, PR",
"foa_section_content_id": 150
},
{
"issuer_id": 2639,
"Field_of_activity": &开发者_JAVA技巧quot;Jobs",
"id": 162,
"Section": "Branch",
"Content": "Banks, Finance, Insurance",
"foa_section_content_id": 162
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 215,
"Section": "Benefits",
"Content": "Enrolment programme",
"foa_section_content_id": 215
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 220,
"Section": "Benefits",
"Content": "Childcare",
"foa_section_content_id": 220
}
]
}
]
I want to return object like this which contains all the Material as array, Name and there value in array of object like this:
I want to get result using for loop.
[
{
"issuer_id": 2639,
"job_title": "Sales Manager",
"hr-contact": "9767865459",
"adress": "bangalore",
"image": "http://localhost:3003/public/uploads/company-logos/undefined",
"employmentType": [
{
"id": 198,
"Employment_type": "Freelancer"
}
],
"professionalField": [
{
"id": 150,
"Professional_field": "Marketing, Graphics, PR"
}
],
"benefits": [
{
"id": 215,
"Benefits": "Enrolment programme"
},
{
"id": 219,
"Benefits": "Canteen"
},
{
"id": 220,
"Benefits": "Childcare"
},
{
"id": 221,
"Benefits": "Employee events"
},
{
"id": 222,
"Benefits": "Employee mobile phone"
},
{
"id": 223,
"Benefits": "Employee notebook"
},
{
"id": 224,
"Benefits": "Employee bonuses"
}
],
"branch": [
{
"id": 162,
"Branch": "Banks, Finance, Insurance"
}
],
"communes": [],
"positionLevel": [],
"skillSets": [],
"languageSkills": [],
"skillRepository": [],
"jobCluster": [],
"jobClusterDescription": []
}
]
const data = [
{
"issuer_id": 2639,
"job_title": "Sales Manager",
"hr-contact": "9767865459",
"adress": "bangalore",
"image": "http://localhost:3003/public/uploads/company-logos/undefined",
"getEmployerDetail": [
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 111,
"Section": "Communes",
"Content": "Mühlwald",
"foa_section_content_id": 111
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 112,
"Section": "Communes",
"Content": "Wolkenstein in Gröden",
"foa_section_content_id": 112
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 113,
"Section": "Communes",
"Content": "Schnals",
"foa_section_content_id": 113
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 150,
"Section": "Professional field",
"Content": "Marketing, Graphics, PR",
"foa_section_content_id": 150
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 162,
"Section": "Branch",
"Content": "Banks, Finance, Insurance",
"foa_section_content_id": 162
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 215,
"Section": "Benefits",
"Content": "Enrolment programme",
"foa_section_content_id": 215
},
{
"issuer_id": 2639,
"Field_of_activity": "Jobs",
"id": 220,
"Section": "Benefits",
"Content": "Childcare",
"foa_section_content_id": 220
}
]
}
]
const getGroupWithFor = (data) =>{
const result = [];
for (let i = 0; i < data.length; i++) {
const detail = data[i].getEmployerDetail;
delete data[i].getEmployerDetail;
const group = data[i];
for (let j = 0; j < detail.length; j++) {
const section = detail[j].Section;
const current = {
[section]: detail[j].Content,
id: detail[j].id
}
if (group[section]) {
group[section].push(current)
} else {
group[section] = [current]
}
}
result.push(group)
}
return result
}
const getGroupWithForEach = (data) => {
const result = []
data.forEach(detail => {
const employerDetail = detail.getEmployerDetail;
delete detail.getEmployerDetail;
const group = detail;
employerDetail.forEach(eachDetail => {
const section = eachDetail.Section;
const current = {
[section]: eachDetail.Content,
id: eachDetail.id
}
if (group[section]) {
group[section].push(current)
} else {
group[section] = [current]
}
})
result.push(group)
})
return result;
}
console.log(JSON.stringify(getGroupWithForEach(data), null, 2))
精彩评论