0

Hi i dont't know how to compare each elements of the array to the state:

enter image description here

doesCustomerExist = () => {
  const { customers } = this.props;
  let result = customers.map(c => c.phone)
  if(result === this.state.phone)
    return (console.log('already exist'))
    console.log(result)
}
2
  • is this.state.phone an array or a single element (string)? Commented Jun 11, 2019 at 13:47
  • Please edit the question with more information. Which var is the array you wanna compare? Put in comments the value of the state and the log of 'result'. Commented Jun 11, 2019 at 13:53

2 Answers 2

1

You can use indexOf to check if an array contains a given element.

For instance:

const phone = "123";
const customers = ["111", "222", "333", "123"];

const isInArray = customers.indexOf(phone) > -1;

console.log(isInArray);

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

Comments

1

For this simply use Array.inlcudes:

const phone = "123";
const customers = ["111", "222", "333", "123"];

const isFound = customers.includes(phone)

console.log(isFound);

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.