【问题标题】:how can I match a username with the key of a property in an object which is in an array?如何将用户名与数组中对象中的属性键匹配?
【发布时间】:2019-05-25 00:20:18
【问题描述】:

我有一个包含对象的数组。我想从客户端获取用户名并检查该用户名是否存在于对象的键中。 但我不知道如何将该用户名与对象的键匹配!

这是我的代码的简单形式:

var usersArray = [
    {
        name : "Simon",
        password : 123
    },
    {
        name :"David",
        password : 456
    }
];

【问题讨论】:

  • 不想想要将凭据返回到前端...
  • 你为什么要使用数组?如果您使用的是数据库,只需通过适当的调用查询数据库,如果不是:这绝对不是您使用用户凭据的方式,您应该找到一个已建立的用户管理包,而不是滚动您难以置信的不安全自己的包。跨度>

标签: javascript jquery node.js


【解决方案1】:

要检查数组是否包含项目,您可以使用Array.prototype.someArray.prototype.everyArray.prototype.findfor 循环。

以下是全部 4 种方式:

var usersArray = [
    {
        name : "Simon",
        password : 123
    },
    {
        name :"David",
        password : 456
    }
];

const userNameFromClient = 'David';

// use a `for of` loop (using a `while`, or a regular `for` loop would also work)

// A loop allows us to iterate through an entire collection.
// Once we find an item satisfying a condition, we can signal that.

// This is a good starting point that explicitly
// shows the mechanics of looping over a collection
// to find an item satisfying a condition
for (const user of usersArray) {
  if (user.name === userNameFromClient) {
    console.log(true);
  }
}

// Since this operation is quite frequent in programming,
// most languages give us tools and methods to achieve
// above in a more declarative fashion and with less code

// For your particular case, the most common approach
// would be to use the `some` method on the array.

// We want to return true if there exists at least one item
// in our array that satisfies our condition

// Code bellow will return true if one object in `usersArray`
// has a property `name` whose value is a string
// equal to the string contained in the
// `userNameFromClient` variable
console.log(
  usersArray.some(user => user.name === userNameFromClient)
)

// Another option is to use the find method.

// Since `find` will return the actual user object
// that satisfies a condition instead of a boolean `true` or `false`,
// we would have to convert the returned result
// into a Boolean ourselves
console.log(
  Boolean(usersArray.find(user => user.name === userNameFromClient))
)


// The `some` method also has a cousin called `every`
// which can be used in this situation but is a little
// bit messy because it requires multiple negations

// You can see what I mean if we explain the operation
// in plain English:

// If not every item in a given array does not satisfy a condition
// return true

// Above is just a convoluted way of saying if there exists
// at least one item satisfying a condition, return true

// Code bellow will return true if one object in `usersArray`
// has a property `name` whose value is a string
// equal to the string contained in the
// `userNameFromClient` variable
console.log(
  !usersArray.every(user => user.name !== userNameFromClient)
)

【讨论】:

  • 哪一种会是最“典型”的方式?
【解决方案2】:

var usersArray = [{
  name: "Simon",
  password: 123
}, {
  name: "David",
  password: 456
}];

let name = "David";

let result = usersArray.some(x => x.name === name)
console.log(result)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-12
    • 2022-09-23
    • 2021-12-03
    • 2022-11-21
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    相关资源
    最近更新 更多