Skip to content

Instantly share code, notes, and snippets.

@SurenderLohia
Created October 13, 2022 05:40
Show Gist options
  • Save SurenderLohia/6f8b146c274fe61e99accd1698faff63 to your computer and use it in GitHub Desktop.
Save SurenderLohia/6f8b146c274fe61e99accd1698faff63 to your computer and use it in GitHub Desktop.
const students = [
{
id: '1',
isSelected: true,
name: 'Mike',
},
{
id: '2',
isSelected: false,
name: 'Jane',
},
{
id: '3',
isSelected: false,
name: 'Suren',
},
];
let updatedStudents = [...students];
const handleAssignStudentChange = (studentId, isSelected) => {
updatedStudents = updatedStudents.map((student) => {
if (student.id === studentId) {
return {
...student,
isSelected: isSelected,
};
}
return student;
});
};
const getIsChanged = (students, updatedStudents) => {
const studentsLen = students.length;
for (let i = 0; i < studentsLen; i++) {
if (students[i].isSelected !== updatedStudents[i].isSelected) {
return true;
}
}
return false;
};
console.log(updatedStudents);
let isChanged = getIsChanged(students, updatedStudents);
console.log({ isChanged }); // false
handleAssignStudentChange('2', true);
isChanged = getIsChanged(students, updatedStudents);
console.log(updatedStudents);
console.log({ isChanged }); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment