Skip to content

Instantly share code, notes, and snippets.

@SurenderLohia
Created October 13, 2022 05:42
Show Gist options
  • Save SurenderLohia/f3be1e562d61586e4ec17d3ca52094f1 to your computer and use it in GitHub Desktop.
Save SurenderLohia/f3be1e562d61586e4ec17d3ca52094f1 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',
},
];
const allStudents = {};
students.forEach((item) => {
allStudents[item.id] = item;
});
let updatedStudents = { ...allStudents };
const handleAssignStudentChange = (studentId, isSelected) => {
updatedStudents[studentId] = {
...updatedStudents[studentId],
isSelected,
};
};
const getIsChanged = (students, updatedStudents) => {
const studentsIds = Object.keys(students);
const studentsLen = studentsIds.length;
for (let i = 0; i < studentsLen; i++) {
const id = studentsIds[i];
console.log(
`${students[id].isSelected}: ${updatedStudents[id].isSelected}`
);
if (students[id].isSelected !== updatedStudents[id].isSelected) {
return true;
}
}
return false;
};
console.log(updatedStudents);
let isChanged = getIsChanged(allStudents, updatedStudents);
console.log({ isChanged }); // false
handleAssignStudentChange('2', true);
isChanged = getIsChanged(allStudents, 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