动态将对象添加到数组

时间:2019-09-29 08:04:54

标签: javascript

我想通过对象数组为表动态创建数据源。

所需的数据源值:

values = [
    {
        name: "Super Admin"
        cv: 1
        assessment1_title: score/status
        assessment2_title: score/status
        interview1_title: score/status
        interview2_title: score/status
    }
]

我有以下对象数组:

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

其中的{_1}将动态生成评估标题的值,而data.assessments.map(({title}) => title)将是得分和状态之一

value

我想获得所需的数据源值。预先感谢

2 个答案:

答案 0 :(得分:1)

您可以通过在候选人上使用reduce然后在forEach评估元素中添加标题和得分/状态来获得所需的结果。

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

const result = data.candidates.reduce((r, c) => {
  const obj = {}
  obj.cv = c.user_id;
  obj.name = c.user_name;
  c.assessments.forEach((e, i) => {
    const {score, status: {name}} = e;
    const {title} = data.assessments[i];
    obj[title] = e.status.id === 22 ? name : score;
  })
  r.push(obj)
  return r;
}, [])

console.log(result)

您还可以创建一个更灵活的解决方案,以防原始对象中有两个以上键的情况下使用。

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

const result = data.candidates.reduce((r, c) => {
  const obj = {}
  obj.cv = c.user_id;
  obj.name = c.user_name;
  
  Object.keys(data).forEach(k => {
    if(k !== 'candidates') {
      data[k].forEach((e, i) => {
        const {title} = e;
        const {score, status: {name}} = c[k][i];
        obj[title] = `${score}/${name}`
      })
    }
  })
  
  r.push(obj)
  return r;
}, [])

console.log(result)

答案 1 :(得分:1)

我的解决方案如下。

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: 6,
          score: 5,
          duration: 1170,
          status: {
            id: 22,
            name: "completed"
          }
        },
        {
          id: 7,
          score: 0,
          duration: 0,
          status: {
            id: 21,
            name: "Pending"
          }
        }
      ]
    }
  ]
};

assessments_map = data.assessments.reduce((acc, val) => {
    const {id,...rest} =  val;
    acc[id] = rest;
    return acc
}, {});

assessments_map;


a = data.candidates.map((candidate) => {
    return {name: candidate.user_name,
        ...candidate.assessments.reduce((acc, assessment) => {
            acc[assessments_map[assessment.id].title] =
              assessment.status.id == 22
                ? assessment.score
                : assessment.status.name;         
            return acc;
        }, {})
    }
});
console.log(a);