I have an array
with multiple objects
inside.
It's structured like that:
const Instructor = [
{ ID: '141',
InstructorNameAR: 'test',
InstructorNameEN: 'Mohamed Ahmed',
InstructorBriefAR: 'phd in chemistry',
InstructorBriefEN: 'phd in chemistry' },
{ ID: '140',
InstructorNameAR: 'test',
InstructorNameEN: 'Mahmoud Ahmed',
InstructorBriefAR: 'phd in chemistry',
InstructorBriefEN: 'phd in chemistry' },
]
I wanted to add other objects
but filtered of duplicates based on their ID
values.
Example of objects
i want to add :-
const InstructorInstance = {
ID: 'ID',
InstructorNameAR: 'NAMEAR',
InstructorNameEN: 'NAMEEN',
InstructorBriefAR: 'BRIEFAR',
InstructorBriefEN : 'BRIEFEN'
}
I used this method to filter by ID
.
But it didn't work as it compares only a single
value of the array
to the value i provided. which means it might be a duplicated object
but still gets added because it did not check if it exists in each array element
Instructor.forEach(instance =>{
if(instance.ID !== InstructorInstance.ID){
Instructor.push(InstructorInstance);
}else{
console.log('Duplicate')
}
})