student:any=[{name:'shariful', id:'1',teacherId:'1'},{name:'Hasan', id:'2',teacherId:'2'},{name:'sohag', id:'3',teacherId:'2'}]
teacher:any=[{name:'Robi',id:'1'},{name:'Aktarujaman',id:'2'}]
needResult:any=[{name:'shariful', id:'1',teacherId:1,teacherName:'Robi'},{name:'Hasan', id:'2',teacherId:'2',teacherName:'Aktarujaman'},{name:'sohag', id:'3',teacherId:'1',teacherName:'Robi'}]
I need to join student and teacher . the output is as like as needResult .
Need to join in my angular project
2
Answers
Just map student, and add the teacherName by finding the corresponding teacher in the teacher array:
For linear time complexity, you can first create an object (or
Map
) that associates each teacher id with the teacher name. Then, you can useArray#map
to create a new array where the teacher name is added to each element.