Home > Software design >  Create an array of object by mapping and joining two array of objects
Create an array of object by mapping and joining two array of objects

Time:11-11

I have an array of objects like this

const data = [{
    "id": 1,
    "content": {
      "type": "img",
      "state": "rejected"
    },
    "entity": {
      "type": "student",
      "studentID": [
        44
      ]
    },
    "status": "rejected"
  },
  {
    "id": 2,
    "content": {
      "type": "img",
      "state": "approved"
    },
    "entity": {
      "type": "student",
      "studentID": [
        45
      ]
    },
    "status": "approved"
  },
  {
    "id": 3,
    "content": {
      "type": "img",
      "state": "approved"
    },
    "entity": {
      "type": "student",
      "studentID": [
        44
      ]
    },
    "status": "approved"
  }
]

As you can see, we have 2 objects of student id 44 (one with status rejected and one with approved) and one of student id 45. I have another array of object with student info like this

const students = [{
    student_id: 44,
    fname: 'student 1',
    lname: '.',
    school: 'XYZ',
  },
  {
    student_id: 45,
    fname: 'student 2',
    lname: '.',
    school: 'ABC',
  }
]

Now, i want to create a new array of object with both of them mapped, but each object in data mapped with their corresponding student (relation between entity.studentID[0] in data with student_id in students, so that the resultant array of object is

[{
    student_id: 44,
    fname: 'student 1',
    lname: '.',
    school: 'XYZ',
    item: {
      id: 1,
      status: "rejected"
    },
  },
  {
    student_id: 45,
    fname: 'student 2',
    lname: '.',
    school: 'ABC',
    item: {
      id: 2,
      status: "approved"
    },
  },
  {
    student_id: 44,
    fname: 'student 1',
    lname: '.',
    school: 'XYZ',
    item: {
      id: 3,
      status: "approved"
    },
  },
]

What i did was i ran a loop on students and tried using map but that returns me an array of objects that qualify the condition instead of the objects themselves.

let arr = []
for (let student of students) {
  let obj = { ...student
  };
  obj["item"] = data.map((p) => {
    if (p.entity.studentId[0] === student.student_id) {
      return {
        id: p.id,
        status: p.status,
      };
    }
  });
  arr.push(obj);
}

Where am i going wrong and what should i do instead?

CodePudding user response:

Your code is a bit complex,below is a more simple solution for you

let result = data.map(d =>{
  let stu = students.find(i => d.entity.studentID.includes(i.student_id))
  return {...stu,item:{id:d.id,status:d.status}}
})
console.log(result)

For your code,the reason is that map will return undefined if it not meets p.entity.studentId[0] === student.student_id

let arr = []
for (let student of students) {
  let obj = { ...student
  };
  obj["item"] = data.map((p) => {
    if (p.entity.studentId[0] === student.student_id) {
      return {
        id: p.id,
        status: p.status,
      };
    }
    // will return undefined when not meet the if condition
  });
  arr.push(obj);
}

Working code

const data = [{
    "id": 1,
    "content": {
      "type": "img",
      "state": "rejected"
    },
    "entity": {
      "type": "student",
      "studentID": [
        44
      ]
    },
    "status": "rejected"
  },
  {
    "id": 2,
    "content": {
      "type": "img",
      "state": "approved"
    },
    "entity": {
      "type": "student",
      "studentID": [
        45
      ]
    },
    "status": "approved"
  },
  {
    "id": 3,
    "content": {
      "type": "img",
      "state": "approved"
    },
    "entity": {
      "type": "student",
      "studentID": [
        44
      ]
    },
    "status": "approved"
  }
]

const students = [{
    student_id: 44,
    fname: 'student 1',
    lname: '.',
    school: 'XYZ',
  },
  {
    student_id: 45,
    fname: 'student 2',
    lname: '.',
    school: 'ABC',
  }
]


let result = data.map(d =>{
  let stu = students.find(i => d.entity.studentID.includes(i.student_id))
  return {...stu,item:{id:d.id,status:d.status}}
})

console.log(result)

CodePudding user response:

  • Try this if it's working

let newStudent = [];
data.map((data) => {
    const studentId = data['entity']['studentID'];
    function sts() { return data.status};
    students.map((student) => {
        const std_id = student.student_id;
        if (std_id == studentId) {
            newStudent.push({
                student_id: std_id,
                fname: student.fname,
                lname: student.lname,
                school: student.school,
                id: newStudent.length   1,
                status:  sts()
            });

        }
        
    })
})

  • Related