【问题标题】:How to access the value of the key inside the Jsonfile with for loop如何使用 for 循环访问 Jsonfile 中键的值
【发布时间】:2020-02-07 11:40:30
【问题描述】:

我正在尝试在此 for 循环中访问用户名。我想将请求中的用户名与文件中的用户名进行比较。在我下面写的 for 循环中,我达到了 ""(let user in users)" 这个 "1580996730372" 的键 键。我需要做什么才能访问文件中的用户名。我已经尝试过 Object.values() 和 Object.keys() 但也许我正在做 w

FOR 循环


    let users = JSON.parse(data);

    for (let user in users) {
       if (JSON.stringify(users[user]) === JSON.stringify(req.body)) {
         res.status(200).send({ [user]: users[user] });
       return;
    }

JSON 数据

    {
    "1580996730372": {
        "id": "f08dbc40-48e6-11ea-bbe9-c762eb24fe1f",
        "username": "D",
        "password": "kkk333"
    },
    "1580996733810": {
        "id": "f29a5520-48e6-11ea-b91e-fb39bf5103d1",
        "username": "Dskskk",
        "password": "kkk333"
    },
    "1581009019213": {
        "id": "8d460fd0-4903-11ea-9b24-1bb0f34ace07",
        "username": "Dkdkd",
        "password": "vvvkxkkffkk748484"
    },
    "1581009170013": {
        "id": "e72850d0-4903-11ea-ae92-c3bd48f3469a",
        "username": "Dkdkdjjj",
        "password": "hhjh7666"
    },
    "1581068163542": {
        "id": "420a7760-498d-11ea-b90d-4d96b87b7fc1",
        "username": "İbotest",
        "password": "skskkssk377338"
    }
}

【问题讨论】:

    标签: javascript json for-loop filesystems


    【解决方案1】:

    如果user 是类似"1580996730372" 的值,那么

    • users[user]代表key为"1580996730372"的对象
    • users[user]['username']username 键的值

    【讨论】:

      猜你喜欢
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 2016-03-27
      相关资源
      最近更新 更多