Created
October 13, 2022 05:40
-
-
Save SurenderLohia/6f8b146c274fe61e99accd1698faff63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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