Hello guys I have a doubt regarding JavaScript Objects!. Thanks in advance for any future answers. I will explain my situation here. 1)I have 3 objects named courses,admission_details,attendance. 2)I need to create another object from this 3 objects.
const COURSES=[
{
PROGRAM: "BTech",
COURSE: 'PHYSICS',
FEE:200
},
{
PROGRAM: "BTech",
COURSE: 'CHEMISTRY',
FEE:200
},
{
PROGRAM: "BTech",
COURSE: 'BIOLOGY',
FEE:200
}
]
const ADMISSION_DETAILS=[
{
NAME:'AJU', REGISTER_NUMBER:'JAJEGJAFD512315',
EMAIL:'[email protected]',
PHONE:'974654645454'
}
]
const ATTENDANCE = [
{
NAME: 'AJU',
REGISTER_NUMBER: 'JAJEGJAFD512315',
ATTENDANCE:{
PHYSICS:75,
CHEMISTRY:86,
BIOLOGY:40
}
}
]
i want to create another new object from this 3 array of objects,but i am stuck while doing this.
const desired_output=[
{
NAME:'AJU',
REGISTER_NUMBER:'JAJEGJAFD512315',
EMAIL:'[email protected]',
PHONE:'974654645454',
PROGRAM: "BTech",
COURSE:"PHYSICS",
ATTENDANCE:75,
FEE:200,
},
{
NAME:'AJU',
REGISTER_NUMBER:'JAJEGJAFD512315',
EMAIL:'[email protected]',
PHONE:'974654645454',
PROGRAM: "BTech",
COURSE:"CHEMISTRY",
ATTENDANCE:86,
FEE:200
},
{
NAME:'AJU',
REGISTER_NUMBER:'JAJEGJAFD512315',
EMAIL:'[email protected]',
PHONE:'974654645454',
PROGRAM: "BTech",
COURSE:"BIOLOGY",
ATTENDANCE:40,
FEE:200
}
]
This was the desired output.Please anyone help me to achieve this.
CodePudding user response:
How about this? Loop over ADMISSION_DETAILS, which then find the person, loop over the persons courses, fit everything into an object.
const COURSES=[
{
PROGRAM: "BTech",
COURSE: 'PHYSICS',
FEE:200
},
{
PROGRAM: "BTech",
COURSE: 'CHEMISTRY',
FEE:200
},
{
PROGRAM: "BTech",
COURSE: 'BIOLOGY',
FEE:200
}
]
const ADMISSION_DETAILS=[
{
NAME:'AJU', REGISTER_NUMBER:'JAJEGJAFD512315',
EMAIL:'[email protected]',
PHONE:'974654645454'
}
]
const ATTENDANCE = [
{
NAME: 'AJU',
REGISTER_NUMBER: 'JAJEGJAFD512315',
ATTENDANCE:{
PHYSICS:75,
CHEMISTRY:86,
BIOLOGY:40
}
}
]
const desired_output = ADMISSION_DETAILS.reduce((acc, ad) => {
const at = ATTENDANCE.find(p => p.REGISTER_NUMBER === ad.REGISTER_NUMBER)
return [...acc,...Object.keys(at.ATTENDANCE).map(ck => {
const course = COURSES.find(c => c.COURSE === ck)
return {
...ad,
...course,
ATTENDANCE: at.ATTENDANCE[ck]
}
})]
}, [])
console.log(desired_output)
CodePudding user response:
Basically you need to employ a combination of iterators object/array manipulators. After setting up the outside iterator to work through the array of users, I used each user's ATTENDANCE object as the iterator - by converting it into an array of keys. Along the way, I find the matching data in other variables using Array.find(), then combine objects together using the ... spread operator which allows us to create a copy (not a reference).
const COURSES = [{
PROGRAM: "BTech",
COURSE: 'PHYSICS',
FEE: 200
},
{
PROGRAM: "BTech",
COURSE: 'CHEMISTRY',
FEE: 200
},
{
PROGRAM: "BTech",
COURSE: 'BIOLOGY',
FEE: 200
}
]
const ADMISSION_DETAILS = [{
NAME: 'AJU',
REGISTER_NUMBER: 'JAJEGJAFD512315',
EMAIL: '[email protected]',
PHONE: '974654645454'
}]
const ATTENDANCE = [{
NAME: 'AJU',
REGISTER_NUMBER: 'JAJEGJAFD512315',
ATTENDANCE: {
PHYSICS: 75,
CHEMISTRY: 86,
BIOLOGY: 40
}
}]
let combined = [];
ATTENDANCE.forEach(a => {
let courses = a.ATTENDANCE;
let keys = Object.keys(courses);
keys.forEach(key => {
let obj = { ...a};
delete obj.ATTENDANCE;
// get the actual attendance
obj.ATTENDANCE = courses[key]
// get user details
let user = ADMISSION_DETAILS.find(f => f.REGISTER_NUMBER = a.REGISTER_NUMBER) || {}
let course = COURSES.find(f => f.COURSE = key) || {}
obj = { ...obj,
...user,
...course
}
combined.push(obj)
})
})
console.log(combined)
/*
NAME:'AJU',
REGISTER_NUMBER:'JAJEGJAFD512315',
EMAIL:'[email protected]',
PHONE:'974654645454',
PROGRAM: "BTech",
COURSE:"BIOLOGY",
ATTENDANCE:40,
FEE:200
*/
CodePudding user response:
Yeah this is fine..Very much thankyou..