json node js typescript API从对象数组中创建一个特定模式的对象,我如何使用typescript中的for循环来检索数据?

bn31dyow  于 2022-12-15  发布在  TypeScript
关注(0)|答案(2)|浏览(135)

我在从对象数组创建对象时遇到了for循环中的一些问题。我在node js应用程序中有一个对象:
我正在为rest api工作,必须得到正确的响应。

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

我想返回这样的对象,其中包含所有的材料作为数组,名称和对象数组中的值如下:
我想用for循环得到结果。

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

jmo0nnb31#

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

kg7wmglp2#

这个第一个查询,我们得到了一些数据,我们使用ID附加数据,上面你发送了这个API的逻辑,你能写我在下面的foreach逻辑中使用的逻辑吗?我需要你用那个逻辑写的响应。

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

相关问题