'How to check if the user id exists in the .JSON file

I'm trying to find ID from the .JSON file so I can do a match if statement, current problem is that users are saved in .JSON each time, not just once. So I was looking for a way to find User's ID in the .JSON and then compare it with the statement, but I'm unable to get just the ID from the .JSON(check if the ID exists in that file).

empty .JSON

{
    "users": [
    ]
}

.JSON with collected users

{
    "users": [
        {
            "id": "1",
            "username": "USER1"
        },
        {
            "id": "1",
            "username": "USER1"
        },
        {
            "id": "2",
            "username": "USER2"
        },
        {
            "id": "2",
            "username": "USER2"
        }
    ]
}

EDIT

Code:

let rawuserstats = fs.readFileSync(__dirname + '/users.json', 'utf8');
const userstats = JSON.parse(rawuserstats);
console.log(userstats)


//something above...
const foundUser = userstats.users.find(m => m.id === message.author.id)
console.log('Found user:', foundUser)
if (foundUser !== message.author.id) { //try to match user ID with the .JSON(not working)
//Put user's data to the .JSON 
userstats.users.push({id: message.author.id, username: message.author.username});
fs.writeFileSync(__dirname + '/users.json', JSON.stringify(userstats, 0, 4), 'utf8');
}
//something else...

console.log('Found user:', foundUser) returns undefined if .JSON is empty, then after users are in the .JSON { id: '1', username: 'USER1' }, even if I used USER2 for the command instead. Didn't figure out how to get just the ID so I can compare it...

I tried const foundUser = userstats.users.find(id => id === message.author.id) but this will always return undefined while using console.log('Found user:', foundUser). I'm a bit confused about it.

EDIT

I made some progress const foundUser = userstats.users.find(m => m.id === message.author.id) this will return undefined when the user is not inside of the .JSON, and returns this response when user is in the .JSON { id: '1', username: 'USER1' } or { id: '2', username: 'USER2' } from our example. I just need pick the ID value.



Solution 1:[1]

I found a solution, so posting it right here.

    const foundUser = userstats.users.find(m => m.id === message.author.id)
    console.log('Found user:', foundUser)
    if (!foundUser) {
    //Put user's data in the .JSON 
    userstats.users.push({id: message.author.id, username: message.author.username});
    fs.writeFileSync(__dirname + '/users.json', JSON.stringify(userstats, 0, 4), 'utf8');
    }

As foundUser returns undefined, I was able to use this kind of statement and it works, users are not duplicating in .JSON file anymore.

But I would still like to know, how I can get just the ID response (in this case 1) instead of { id: '1', username: 'USER1' }, is that possible via .find?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RasmonT