Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react: array "contains" method

I have a react class component, which has in its state a parameter object of type "credentials" :

{ user: '' , password: '' }.

I add new "credentials" to a "credentialList" array also in the state and output it. I am trying to make the program give me an error when I try to enter a new "credentials" with a user that already exists in the list. I gave each "credentials" a key value equal to user. Unfortunately this has not worked. I marked the problematic if statement in ** **. Thanks to any helper. My code:

state = {
    user: '',
    password: '',
    clicked: false,
    credentialsList: []
}

userChangedHandler = (event) => {
    this.setState( { user: event.target.value })
}

passwordChangedHandler = (event) => {
    this.setState( { password: event.target.value })
}

userSubmitHandler = () => {
    if(this.state.user === '' || this.state.password === ''){
        alert('Please enter username and password!');
    }
    const credential = {user: this.state.user , password: this.state.password};
    **if(this.state.credentialsList.length !== 0 && this.state.credentialsList.contains(credential)){
        alert('Enter New')
    }**
    else {
        let newList = [...this.state.credentialsList, {user: this.state.user, password: this.state.password }];
        this.setState({clicked: true, user: '', password: '', credentialsList: newList})
    }
}

formEraseHandler = () => {
    if(this.state.credentialsList.length === 0){
        alert('The User List is Empty!')
    }
    else {
        let newList = [];
        this.setState({ credentialsList: newList, user:'', password:''});
    }
}

render() {
    return(
     <div>
        <Form
            credentialsList={this.state.credentialsList}
            user={this.state.user}
            password={this.state.password}
            clicked={this.state.clicked}
            userChanged={(event) => this.userChangedHandler(event)}
            passwordChanged={(event) => this.passwordChangedHandler(event)}
            addButtonClicked={this.userSubmitHandler}
            resetButtonClicked={this.formEraseHandler}/>
        <CredentialsList credentials={this.state.credentialsList}/>
     </div>
    )
}

}

like image 500
Uri Shapira Avatar asked Nov 02 '18 07:11

Uri Shapira


People also ask

How do you check if array contains value in React?

To check if an element exists in an array in React: Use the includes() method to check if a primitive exists in an array. Use the some() method to check if an object exists in an array.

How do you use includes in array of objects in React JS?

Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.

How do you check if an ID exists in an array?

The indexof() method in Javascript is one of the most convenient ways to find out whether a value exists in an array or not. The indexof() method works on the phenomenon of index numbers. This method returns the index of the array if found and returns -1 otherwise.


2 Answers

There is no contains method but you can use some()

Here is an example:

const exists = this.state.credentialsList.some(v => (v.user === credential.user && v.password === credential.password));

The code above checks if any of the objects inside the credentialsList array has the same user and password properties values as the credential object. You can modify the if condition if you don't want to check for both these properties.

like image 106
Titus Avatar answered Sep 24 '22 15:09

Titus


I think the method you are looking for is includes

This will compare the entire object with the one given in params. For example:

const credentials = [{ username: 'foo', password: 'bar' }];
credentials.includes({ username: 'foo, password: 'bar' }); // true
credentials.includes({ username: 'foo, password: 'foobar' }); // false

If you want to compare specific values then the some method is more appropriate

credentials.some(cred => cred.username === this.state.username); // true
like image 31
Vincent Newkirk Avatar answered Sep 21 '22 15:09

Vincent Newkirk