Hello, my problem is that I go through all the users that I have in my rest api, and I want that once it is doing the tour, if it detects that there is one like it, it sends me a message that that user already exists and that it stops continuing with the for loop . And if that user does not exist, execute that for loop and insert that user at the end.
My question is if it is possible to do what I have mentioned above. And if you could, could you tell me how it would be to stop that loop. I'm using Ionic 3 , Angular 5 and Slim as back-end api-rest in case the function is relevant depending on what I use.
Thanks in advance.
guardarNicknameUsuario() {
this.userService.getUsers()
.subscribe(
(data) => { // Success
this.users = data;
console.log(data);
for (let i = 0; i < this.users.length; i++) {
console.log(this.users[i].nickname);
if (this.users[i].nickname === this.nicknameUsuario.value.nickname) {
// alert("Este usuario existe ya, porfavor introduce otro nickname");
let alert = this.alertCtrl.create({
title: 'Este usuario ya existe',
subTitle: 'Introduzca otro nickname',
buttons: ['Ok']
});
alert.present();
} else {
// alert("por que entras");
this.userService.postDatos(this.nicknameUsuario.value.nickname);
console.log(this.nicknameUsuario.value.nickname);
window.localStorage['nickUsuarioAplicacion'] = this.nicknameUsuario.value.nickname;
this.navCtrl.push(TabsPage);
}
}
},
(error) => {
console.error(error);
}
)
}
Yes you can stop a for loop, use
break
Reference
Any repetitive structure can be stopped with the keyword break , which means exactly that, to interrupt an action.