如何在JS中检查对象数组是否包含一个对象?

时间:2021-01-04 07:30:30

I have this code:

我有这段代码:

    var users = [];
users.push({
    username: "admin",
    password: "admin"
});
this.showAllUsers = function() {
    console.log(users);
};
this.addUser = function(user) {
    if('username' in user && 'password' in user) {
        users.push({
            username: user.username,
            password: user.password
        })
    }
    else {
        throw "Invalid object";
    }
};
this.isExist = function(user) {
    console.log(users.indexOf({
        username: "admin",
        password: "admin"
    }));

};

Why console log prints all the time -1 ? Users array contains array with object with property username: "admin: and password: "admin".

为什么控制台日志总是打印-1 ?用户数组包含带有属性用户名的对象:“admin:和密码:admin”。

3 个解决方案

#1


0  

The indexOf method uses strict equals (===). When you use {foo: bar}, you create a new (unique) object that will not evaluate strictly equal to anything already in the array.

indexOf方法使用严格的equals(===)。当您使用{foo: bar}时,您将创建一个新的(惟一的)对象,该对象不会严格地计算为数组中已经存在的任何值。

What you should do is iterate through the array and use a function to compare each object using deep equality. Simply, you could use something like:

您应该做的是遍历数组并使用一个函数使用深度相等来比较每个对象。简单地说,您可以使用以下内容:

function match(arr, ref) { // Take an array and a reference object
  return arr.filter(function (cur) { // Select any items in the array...
    return Object.keys(ref).every(function (key) { // Where all keys...
      return ref[key] === cur[key]; // Have the same value in the item and reference
    });
  });
}

#2


0  

Take a look here indexOf.
And remember that managing passwords on the client side is security flaw.

看一下索引。记住,在客户端管理密码是安全缺陷。

#3


0  

this.isExist = function(user) {
    console.log(users.indexOf({
        username: "admin",
        password: "admin"
    }));
};

The problem here is that you're checking if the users array contains this new object you're creating. Since it's a new object, it's obviously not going to contain it. You need to check to see if the users object contains any object that has a username or password equal to "admin". Maybe something like

这里的问题是您正在检查用户数组是否包含您正在创建的新对象。因为它是一个新对象,它显然不会包含它。您需要检查用户对象是否包含任何用户名或密码等于“admin”的对象。也许就像

this.isExist = function(user) {
    for(var x = 0; x < users.length; x++) {
        var current = users[x];
        if(current.username === user.username && current.password === user.password) {
            return true;
        }
    }

    return false;
};

Or, alternatively, you can simply use Linq.js, which helps a lot with these kind of queries. It's one of my favorite libraries.

或者,您也可以简单地使用Linq。js,这对查询很有帮助。这是我最喜欢的图书馆之一。

https://linqjs.codeplex.com/

https://linqjs.codeplex.com/

#1


0  

The indexOf method uses strict equals (===). When you use {foo: bar}, you create a new (unique) object that will not evaluate strictly equal to anything already in the array.

indexOf方法使用严格的equals(===)。当您使用{foo: bar}时,您将创建一个新的(惟一的)对象,该对象不会严格地计算为数组中已经存在的任何值。

What you should do is iterate through the array and use a function to compare each object using deep equality. Simply, you could use something like:

您应该做的是遍历数组并使用一个函数使用深度相等来比较每个对象。简单地说,您可以使用以下内容:

function match(arr, ref) { // Take an array and a reference object
  return arr.filter(function (cur) { // Select any items in the array...
    return Object.keys(ref).every(function (key) { // Where all keys...
      return ref[key] === cur[key]; // Have the same value in the item and reference
    });
  });
}

#2


0  

Take a look here indexOf.
And remember that managing passwords on the client side is security flaw.

看一下索引。记住,在客户端管理密码是安全缺陷。

#3


0  

this.isExist = function(user) {
    console.log(users.indexOf({
        username: "admin",
        password: "admin"
    }));
};

The problem here is that you're checking if the users array contains this new object you're creating. Since it's a new object, it's obviously not going to contain it. You need to check to see if the users object contains any object that has a username or password equal to "admin". Maybe something like

这里的问题是您正在检查用户数组是否包含您正在创建的新对象。因为它是一个新对象,它显然不会包含它。您需要检查用户对象是否包含任何用户名或密码等于“admin”的对象。也许就像

this.isExist = function(user) {
    for(var x = 0; x < users.length; x++) {
        var current = users[x];
        if(current.username === user.username && current.password === user.password) {
            return true;
        }
    }

    return false;
};

Or, alternatively, you can simply use Linq.js, which helps a lot with these kind of queries. It's one of my favorite libraries.

或者,您也可以简单地使用Linq。js,这对查询很有帮助。这是我最喜欢的图书馆之一。

https://linqjs.codeplex.com/

https://linqjs.codeplex.com/

相关文章