1

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?

3
  • PLease add a minimal reproducible example snippet and the desired outcome. Commented Nov 11, 2022 at 10:40
  • map returns an Array, so if item is supposed to be a plain Object, why are you setting it to the result of data.map()? Your entire resulting object (and not the item property) looks like a mapping from data, perhaps followed by a filtering using students. Commented Nov 11, 2022 at 10:44
  • @SebastianSimon i know. But i think i need the map to loop over data and determine which object matches its corresponding student. I know i should not return the map but then how should i return the object? Commented Nov 11, 2022 at 10:50

2 Answers 2

2

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)

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

Comments

0

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

Comments

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.