Home > Net >  node js typescript api create an object in specific pattern from array of object, How can i retrive
node js typescript api create an object in specific pattern from array of object, How can i retrive

Time:12-07

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": "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": []
}
]

CodePudding user response:

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))

CodePudding user response:

This First Query we are getting some data and we are attaching with data using ID , above you sent the logic for this api can you write the logic i have used in foreach logic below.I need the response which you wrote above using that logic.

const getEmployerByID = async (id: string, jobName: string) => {
        const connection = await getConnection();
        try {
            await connection.beginTransaction();
            let getPremiumEmployer = `SELECT DISTINCT rifp.issuer_id,rifp.foa_property_id,fpl.name,rifp.text_value as "company name",rifp.text_value,
            rifp.int_value,rifp.float_value, fp.datatype FROM rel_account_issuer rai inner join account on account.account_id=rai.account_id inner join 
             rel_issuer_foa_property rifp join foa_property_localization fpl on rifp.foa_property_id=fpl.foa_property_id  inner join foa_property fp on 
             fp.foa_property_id=rifp.foa_property_id where rifp.issuer_id = ${id} and fpl.name IN ('job title','company_name',
             'adress','image','job type','company_youtube','cover image',
             'company_facebook','company_linkedin','company_instagram','overview','hr-contact','number_of_employees','company_gallery','section_description')  order by rifp.foa_property_id;`
            const [employerRes]: any = await connection.execute(getPremiumEmployer, [id]);
            let [getEmployerDetail]: any = await connection.query(`SELECT DISTINCT rifsc.issuer_id,fl.name AS 'Field_of_activity',fscl.foa_section_content_id AS 'id', 
            fsl.name AS 'Section' , fscl.name AS 'Content', rifsc.foa_section_content_id FROM foa f 
            JOIN foa_section fs USING (foa_id) JOIN foa_localization fl USING (foa_id)  
            JOIN foa_section_content fsc USING (foa_section_id) 
            JOIN foa_section_localization fsl USING (foa_section_id) 
            JOIN foa_section_content_localization fscl USING (foa_section_content_id) 
            JOIN rel_issuer_foa_section_content rifsc USING (foa_section_content_id)   inner join
            rel_account_issuer rai inner join account on account.account_id=rai.account_id  WHERE rifsc.issuer_id =${id} `)  
            let values = employerRes.reduce((prev: any, curr: any) => ({
                ...prev, [curr.issuer_id]: {
                    issuer_id: curr.issuer_id,
                    ...prev[curr.issuer_id], [(curr.name).replaceAll(' ', "_")]: curr[`${curr.datatype}_value`]
                }
            }), {} as Record<string, any>)
            const jobCluster: any = []
            const jobClusterDescription: any = []
            const employer =["Benefits","Communes"]
            Object.values(values).forEach((x: any) => {
                Object.values(getEmployerDetail).forEach((Benefit: any) => {
                    if (x.issuer_id == Benefit.issuer_id) {
                        Object.assign(values[id],{getEmployerDetail} )
                    }
                })
            })     
        } catch (error) {
            throw error
        }
    }
  • Related