komal deep singh chahal komal deep singh chahal - 3 months ago 8
React JSX Question

IF ELSE in react(If the condition is correct, take me to another page)

I am storing some data in array.
which is gonna be like this

datas[
password:"dfsdfsd",
username : "dfgdgdfg55555",

password:"deep",
username : "dfgdgdfg56556",

password:"hello",
username : "dfgdgdfg65",

password:"hello1234",
username : "dfgdgdfg123"
]


now i want to camapare the username.
and i am trying like it and want if the condition is true(take me to another page) but don't know how to do that.

checkCred(){
var details_array = this.state.datas1 ;
console.log(details_array);
for(var i=0; i < details_array.length; i++){
var currentObject = details_array[i];
if (this.email.value === currentObject.username){
console.log('yes done it');
window.location = '/Index';// this is not working and think window.location is stupid methed...
// please guide me if you have any better idea than this
}

}
}
<input ref={(e) => this.email = e} name="email" type="email" /><br/>


am i doing correct way or not please guide me.

Answer

Make following changes to work as per expectations,

var details_array = this.state.datas ;
for(var i=0; i < details_array.length; i++){
 var currentObject = details_array[i];
 if (this.email.value === currentObject.username){
    console.log('yes done');
 }
}

Note: hope you have data in details_array.

Comments