I have two Arrayes that has been sourced from two different URIs. I am trying to create a form of code that extracts specific data from one Array and connects it to related data from the other Array. Here's an example:
let coursesData = ['teachers': 'CHCH'];
let teacherData = [
{
'id': {'name': 'CHCH'},
'name':{
'jobtitle': 'Professor',
'firt':'Charie',
'last': Chaplin
},
}];
function renderCourseTeachersList () {
teacherData
.filter(object => {
return object.id.includes(parseInt(coursesData.teachers));
})
.forEach((item, i) => {
console.log(`Lärare: ${item.name.jobtitle} ${item.name.first} ${item.name.last}`);
});
}
I was hoping this would give me a list of the values: jobtitle, first and last in the console log, in order of the matching value: CHCH.
The two arrays are just snippets of a larger source.
Thank you in advance!
As you put a snippet of your source data, I guess coursesData is an array of objects. So I defined multiple objects in coursesData. If your structure is different, Snippet will provide you idea to get the expected result.
let coursesData = [
{
'teachers': 'CHCH'
},
{
'teachers': 'xyz'
},
];
let teacherData = [
{
'id': {
'name': 'CHCH'
},
'name': {
'jobtitle': 'Professor',
'first': 'Charie',
'last': 'Chaplin',
},
},
{
'id': {
'name': 'xyz'
},
'name': {
'jobtitle': 'Lecturer',
'first': 'John',
'last': 'Doe',
},
}
];
function renderCourseTeachersList (){
teacherData.filter(object => coursesData.some(subItem => subItem.teachers == object.id.name))
.forEach((item, i) => {
console.log(`Lärare: ${item.name.jobtitle} ${item.name.first} ${item.name.last}`);
});
}
renderCourseTeachersList();