0

I want to create a datasource dynamically for my table from a array of objects.

Required datasource value:

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

I have the following array of object:

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

where the value of assessment_title will be dynamically generated from data.assessments.map(({title}) => title) and the value will be one of score and status

data.canditates.map(res => res.assessments.map(res2=> {
  if res2.status.id ==22 {
    value = res2.score
  } else {
    value = res2.status.name
  }
})
);

I want to make the required datasource value. Thanks in advance

3
  • if res2.status.id ==22 aren't you getting syntax error here ? Commented Sep 29, 2019 at 8:06
  • why should it be syntax error? btw, I wanted to make the logic clear, but I need the conversion Commented Sep 29, 2019 at 8:13
  • data.canditates.map(res => res.assessments.map(....)) will return an array of arrays, if you want a single level array, use .reduce() instead. Commented Sep 29, 2019 at 8:17

2 Answers 2

1

You can get the desired result by using reduce on candidates and then forEach assessments element add title and score/status.

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)

You could also create a bit more flexible solution that will work in case you have more then two keys in original object.

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)

Sign up to request clarification or add additional context in comments.

Comments

1

My solution would be something like below.

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

1 Comment

Thank you so much for your answer, it was helpful. but I accepted@nenad answer, as it was concise and short. But otherwise, your answer would be accepted. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.