Reputation: 160
I am having issues searching in an array of objects. Basically what my page needs to do is to create a new "client" using information entered by me, such as Full name, User name, Email and Password. Each one of these clients are objects in an array as you can see below.
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"[email protected]","type":"client","password":"jdoe2"},
This client is already created in my js file, what I need to do is to create a new object to add to this array with this same structure. For example,
var clientlist = [{"username":"Peter","fullname":"Peter Jones",
"email":"[email protected]","type":"client","password":"pjones1"},
I have written the code but it doesn't work properly, when I run the Firebug I can see that all elements have been added correctly except for the Username which value is "". I cannot seem to search for the username to see if the username that I am adding already exists, it may be a syntax mistake. I will leave my complete code below and thanks in advance for the assistance!.
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"[email protected]","type":"client","password":"jdoe2"},
var Client = {};
function NewClient(){
var found;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username == user) {
found = true;
}else{
found = false;
}
}
if (found == true){
$("#msj").html("User already exists!");
}
else if(found == false){
Client["fullname"] = $("#fullname").val();
Client["username"] = user;
Client["email"] = $("#email").val();
Client["type"] = "client";
Client["password"] = $("#password").val();
clientlist[clientlist.length] = Client;
$("#msj").html("New client has been created");
}
}
Upvotes: -1
Views: 83
Reputation: 3570
I guess, you have several issues.
I have corrected them and included it below.
<script>
var clientlist = [{"username":"John","fullname":"John Doe",
"email":"[email protected]","type":"client","password":"jdoe2"}]
function NewClient(){
var found=false;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username==user) {
found = true;
break;
}
}
if (found){
$("#msj").html("User already exists!");
}
else{
var newUser={
fullname:$("#fullname").val(),
username:user,
email:$("#email").val(),
type:"client",
password:$("#password").val()
}
clientlist.push(newUser);
$("#msj").html("New client has been created");
}
}
</script>
Upvotes: 0
Reputation: 251
Few mistakes that you made:
clientlist
arrayThis code below should work correcting a few mistakes that you made along the way.
var clientlist = [{
"username": "John",
"fullname": "John Doe",
"email": "[email protected]",
"type": "client",
"password": "jdoe2"
}];
function NewClient() {
var found = false;
var user = $("#username").val();
for (var i = 0; i < clientlist.length; i++) {
if (clientlist[i].username == user) {
found = true;
} else {
found = false;
}
}
if (found) {
$("#msj").html("User already exists!");
} else {
var newUser = {
fullname: $("#fullname").val(),
username: user,
email: $("#email").val(),
type: "client",
password: $("#password").val()
}
clientlist.push(newUser);
$("#msj").html("New client has been created");
}
}
Made a fiddle for you: http://codepen.io/gabrielgodoy/pen/xOxoWw?editors=1011
Upvotes: 2