0

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.

3
  • 4
    Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet. Also please tag relevant tags. Sounds like you are using React Commented Sep 7, 2021 at 9:43
  • You can use Array methods like .filter() Commented Sep 7, 2021 at 9:44
  • If this is a React question (which it seems like) adding the relevant tags and showing the code of the relevant component (cut down to a minimal example if it's large) would be very helpful. Commented Sep 7, 2021 at 9:45

2 Answers 2

1

If I have understood correctly, you can check that the record isn't the same as your current index inside the find callback like this

var data =[{"name":'Alice', "email":'[email protected]'},{"name":'Bob', "email":'[email protected]'}];
var hasDuplicate =[{"name":'Alice', "email":'[email protected]'},{"name":'Bob', "email":'[email protected]'}, {"name":'Andy', "email":'[email protected]'}]
var currentIndex = 1;
var editing = data[currentIndex];

// duplicate won't be found
if (data.find((record, index) => editing.email == record.email && index != currentIndex))
  console.log("found duplicate");
else 
  console.log("no duplicated found");


// duplicate will be found
if (hasDuplicate.find((record, index) => editing.email == record.email && index != currentIndex))
  console.log("found duplicate");
else 
  console.log("no duplicated found");

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @wraig, that additional check for index was the one I was looking for!
1

Because you know the index of the current value, you can still use the find() method you created on a filtered array :

var currentIndex = 0;
data =[{"name":'Alice', "email":'[email protected]'},{"name":'Bob', "email":'[email protected]'}];
data.filter((_, index) => index != currentIndex).find(...);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.