You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于对象数组动态生成表格所需的指定结构数据源?

Solution to Generate Dynamic Data Source

Here's how you can transform your given data object into the required values array structure, following the specified rules:

Step-by-Step Explanation

  1. Extract Assessment Titles: First, we pull all assessment titles from the main assessments array in your data.
  2. Process Each Candidate: For every candidate, we start building an object with their name and the cv field (note: since no cv data is provided in your input, we use the example value 1—adjust this if you have a dedicated source for cv values).
  3. Map Assessment Scores/Status: For each assessment title, we match it to the candidate's corresponding assessment entry. We use the score if the status ID is 22; otherwise, we use the status name.

Code Implementation (JavaScript)

const data = {
  "assessments": [
    { "id": 6, "title": "PHP Laravel Developer", "type": "Objective" },
    { "id": 7, "title": "Laravel Developer", "type": "Objective" }
  ],
  "candidates": [
    {
      "id": 11, "user_id": 1, "user_name": "Super Admin",
      "assessments": [
        { "id": 1, "score": 5, "duration": 1170, "status": { "id": 22, "name": "completed" } },
        { "id": 2, "score": 0, "duration": 0, "status": { "id": 22, "name": "Pending" } }
      ]
    }
  ]
};

// Extract assessment titles from the main assessments array
const assessmentTitles = data.assessments.map(assess => assess.title);

// Generate the required values array
const values = data.candidates.map(candidate => {
  const candidateObj = {
    name: candidate.user_name,
    cv: 1 // Replace with actual cv data source if available
  };

  // Map each assessment title to its corresponding score/status
  assessmentTitles.forEach((title, index) => {
    const candidateAssessment = candidate.assessments[index];
    candidateObj[title] = candidateAssessment.status.id === 22 
      ? candidateAssessment.score 
      : candidateAssessment.status.name;
  });

  // Note: If you need to add interview fields later, replicate the assessment logic with interview data
  return candidateObj;
});

console.log(values);

Output

Running this code will produce:

[
  {
    "name": "Super Admin",
    "cv": 1,
    "PHP Laravel Developer": 5,
    "Laravel Developer": 0
  }
]

Key Notes

  • CV Field: Since your input data doesn't include cv-related information, we used 1 as in your example. If cv values are stored elsewhere (e.g., in the candidate object), update the cv property to pull from that source.
  • Interviews: Your target structure includes interview fields, but there's no interview data in your input. If you add interview data later, you can replicate the same logic we used for assessments to include those fields.
  • Matching Assessments: We assumed the candidate's assessments are in the same order as the main assessments array. If there's an explicit link (like an assessment_id in the candidate's assessments), adjust the code to match using that ID instead of index.

内容的提问来源于stack exchange,提问作者Md Rafee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:47:44