数组Javascript中的对象问题

时间:2016-05-31 01:44:38

标签: javascript arrays

我在搜索对象数组时遇到问题。基本上我的页面需要做的是创建一个新的"客户端"使用我输入的信息,例如全名,用户名,电子邮件和密码。这些客户端中的每一个都是数组中的对象,如下所示。

var clientlist = [{"username":"John","fullname":"John Doe",
"email":"john.doe@hotmail.com","type":"client","password":"jdoe2"},

此客户端已在我的js文件中创建,我需要做的是创建一个新对象以添加到具有相同结构的此数组。例如,

var clientlist = [{"username":"Peter","fullname":"Peter Jones",
"email":"peter.jones@hotmail.com","type":"client","password":"pjones1"},

我已经编写了代码,但它没有正常工作,当我运行Firebug时,我可以看到所有元素都已正确添加,除了用户名值为""。我似乎无法搜索用户名,看看我添加的用户名是否已存在,可能是语法错误。我将在下面留下完整的代码,并提前感谢您的帮助!

var clientlist = [{"username":"John","fullname":"John Doe",
"email":"john.doe@hotmail.com","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");
}

}

2 个答案:

答案 0 :(得分:2)

你犯的错误很少:

  • 忘记关闭body { width: 94vw; height: 100vw; } video { position: absolute; z-Index: 1; left: 25vw; } canvas { width: 94vw; height: 100vh; position: absolute; z-Index: 0; -webkit-filter: blur(5px); -ms-filter: blur(5px); filter: blur(5px); }数组
  • 忘记实际推送新添加的客户端

下面的代码应该可以纠正您在此过程中犯下的一些错误。

<!DOCTYPE html>
<html>
<body>
  <video width="300px" height="200px;" src="http://mirrors.creativecommons.org/movingimages/webm/ScienceCommonsJesseDylan_240p.webm" autoplay loop></video>
  <canvas></canvas>
  <script>
    window.onload = function() {
      var video = document.querySelector("video");
      var canvas = document.querySelector("canvas")
      var ctx = canvas.getContext("2d");
      video.addEventListener("timeupdate", function(event) {
        ctx.drawImage(this, 0, 0);
      })
    }
  </script>
</body>
</html>

为你做一个小提琴: http://codepen.io/gabrielgodoy/pen/xOxoWw?editors=1011

答案 1 :(得分:0)

我想,你有几个问题。

  1. clientList的结束括号
  2. For循环和找到的变量
  3. 将新用户推送到客户列表。
  4. 我已经纠正了它们并将其包含在下面。

    <script>
    var clientlist = [{"username":"John","fullname":"John Doe",
    "email":"john.doe@hotmail.com","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>