如何在对象数组中搜索-JavaScript [英] How to search in array of objects - javascript

查看:94
本文介绍了如何在对象数组中搜索-JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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":"john.doe@hotmail.com","type":"client","password":"jdoe2"},

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

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":"peter.jones@hotmail.com","type":"client","password":"pjones1"},

我已经编写了代码,但是无法正常工作,我似乎无法搜索用户名以查看所添加的用户名是否已存在,可能是语法错误。我将在下面留下完整的代码,并在此先感谢您的帮助!。

I have written the code but it doesn't work properly, 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":"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] == 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");
    }
}


推荐答案

多个您的 for 循环出现问题。

Several problems with your for loop.

for (var i = 0; i < clientlist.length; i++) {
    if (clientlist[i] == user) {
    found = true;
}else{
    found = false;
}

那么我们在做什么呢? clientlist 是数组,所以 clientlist [i] 是该数组的元素...恰好是一个对象。

So what are we working with? clientlist is array so clientlist[i] is an element of that array...which happens to be an object.

user 值是一个字符串,因此无法在<$ c $中将字符串等同于对象c>如果。

user value is a string so there is no way to equate a string to object in the if.

更正后,将检查对象中正确的属性值:

Correction there would be to inspect the correct property value in object:

if (clientlist[i].username == user) {

下一个问题是,即使找到匹配项,循环也会继续进行。随着循环的继续,找到的将针对每次迭代进行更新。因此,无论是否已确定匹配项,都将仅基于数组中的最后一个对象来设置找到的

Next problem is that the loop keeps going even if a match is found. As loop continues found will be updated for each iteration. Thus found will only be set based on the very last object in array regardless if a match was already determined.

要更正此问题,可以将 for 循环放入函数中,以便在找到匹配项时返回 true 来中断循环。另一种选择是使用其他数组方法,例如 Array.prototype.some(),该方法基于回调中的条件返回布尔值。或者,如果找到匹配项以防止循环继续,请使用 break

To correct this could put that for loop in a function so it breaks the loop by returning true if match is found. Alternative would be use other array methods like Array.prototype.some() which returns a boolean based on conditional in callback. Or use break if a match is found to prevent loop continuing.

break 是最简单的代码插件,所以最终将是

break will be simplest to plugin to the code so final would be

for (var i = 0; i < clientlist.length; i++) {
    if (clientlist[i].username == user) {
    found = true;
    break;// quit loop since we found a match
}else{
    found = false;
}

这篇关于如何在对象数组中搜索-JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆