I'm writing code to check for duplicate values in a form. I need to check that the email shouldn't exist in previous records. Here's how the array(it's initialized in useState) looks -
data =[{"name":'Alice', "email":'[email protected]'},{"name":'Bob', "email":'[email protected]'}]
I had previously written it with array.find() method which worked fine. But my form has an edit option as well. Hence, that creates an error because the value to be edited already exists in data array so returns false.
How can I write a statement that checks for duplicate email but also at same time leaves out the current element we're editing? i.e. The next incoming value shouldn't have same email id as existing -[email protected], [email protected], but at the same time while editing (ex. [email protected]), should allow me to put same value as current but not allow [email protected].
Note, the index of current value is known in local state.
.filter()