javascript 检查 JSON 对象中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31273895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:34:09  来源:igfitidea点击:

Check if value exists in JSON Object

javascriptjsonnode.js

提问by v1shnu

I am learning NodeJS . I am using a JSON Object to check if a user is present.

我正在学习 NodeJS 。我正在使用 JSON 对象来检查用户是否存在。

This is my JSON (users.json):

这是我的 JSON (users.json):

{
    "users": [{
        "fname": "Robert",
        "lname": "Downey Jr.",
        "password": "ironman"
    }, {
        "fname": "Chris",
        "lname": "Evans",
        "password": "cap"
    }, {
        "fname": "Chris",
        "lname": "Hemsworth",
        "password": "thor"
    }, {
        "fname": "Jeremy",
        "lname": "Renner",
        "password": "hawk"
    }]
}

Now I want to pass the fname value of one entry and see if it exists in the JSON Object.

现在我想传递一个条目的 fname 值,看看它是否存在于 JSON 对象中。

This is what I'm doing :

这就是我正在做的:

var file = JSON.parse(fs.readFileSync('users.json', 'utf8'));
for (eachUser in file.users) {
            console.log(eachUser.fname);
        }

But I do not get the fname values but just undefined. I don't know what I am doing wrong.

但我没有得到 fname 值,而只是undefined. 我不知道我做错了什么。

Also is there a way to find if the value exists without having to iterate over the JSON Object ?

还有一种方法可以在不必遍历 JSON Object 的情况下查找值是否存在?

采纳答案by Kristján

The problem is that eachUsergets set to each index in the array - not the object at that index. If you log just eachUser, you'll see

问题是eachUser设置为数组中的每个索引 - 而不是该索引处的对象。如果你只是登录eachUser,你会看到

0
1
2
3

Instead, pull out the object first, and then gets its fname

相反,先拉出对象,然后再获取它的 fname

for (index in file.users) {
  console.log(file.users[index].fname)
}

Here's a demo.

这是一个演示

If you want to do this lookup without iterating through the array, you can re-shape your data into an object with fnameas the key, so it would look like:

如果您想在不遍历数组的情况下进行此查找,您可以将数据重新整形为一个以fname作为键的对象,因此它看起来像:

{
  "Robert": {
    "fname": "Robert",
    "lname": "Downey Jr.",
    "password": "ironman"
  },
  "Chris": {
    "fname": "Chris",
    "lname": "Evans",
    "password": "cap"
  },
  "Jeremy": {
    "fname": "Jeremy",
    "lname": "Renner",
    "password": "hawk"
  }
}

Of course, then either you won't be able to store any duplicate first names, or your need to make the values arrays.

当然,要么您将无法存储任何重复的名字,要么您需要创建值数组。

回答by NiVeR

if ( typeof file !== 'undefined' && file )
{
   //do stuff if file is defined and not null
}

回答by POZ

You have an issue in your iteration statement:

您的迭代语句中存在问题:

for (eachUser in file.users)

Feel free to use one the foreachstatements described in this postsuch as:

随意使用一个foreach中描述的语句这篇文章,如:

file.users.forEach(function(user) {
    console.log(user.fname);
});

Cheers

干杯