3

How would I access the following json data

"users":{
    "2211392761":{"username":"user1"},
    "14300995184":{"username":"user2"},
    "2781554712":{"username":"user3"},
    "3554341":{"username":"user4"},
    "202611":{"username":"user5"},
    "17754300653761":{"username":"user6"}
}

I have this so far and I am aware it is completely wrong:

Object.keys(jsonevents["events"]).forEach(function(key) {
    if(eventName == jsonevents["events"][key]["name"]){
        if(jsonevents["events"][key]["users"]){
            if(jsonevents['events'][key]["users"][message.author.id]){
                delete jsonevents['events'][key]["users"][message.author.id];
                fs.writeFile(eventsjson, JSON.stringify(jsonevents),'utf8');
                sayMessage += "```User is no longer part of the event "+jsonevents['events'][key]["name"]+"```";
            } else {
                sayMessage += "```user is not in the event "+jsonevents['events'][key]["name"]+"```";
            }
        } else {
            sayMessage += "```Why do we have no users```";
        }
    } else {
        //sayMessage += "```No event found```";
    }
});

I need to be able to access the key by passing in the username, so user2 would give me 14300995184 so i can then use this to remove the user from the event.

2 Answers 2

4

You can search through the Object.entries with find() and return the right object. It will return an array of key/value the key will be what you're after:

let users = {
    "2211392761":{"username":"user1"},
    "14300995184":{"username":"user2"},
    "2781554712":{"username":"user3"},
    "3554341":{"username":"user4"},
    "202611":{"username":"user5"},
    "17754300653761":{"username":"user6"}
}

let found = Object.entries(users).find(([key, value]) => value.username === "user2")
console.log(found && found[0]) // found is undefined if not found

Sign up to request clarification or add additional context in comments.

1 Comment

ooh, object.entries - i like that, thank you i will try it out
2

You can iterate over the entries (key-value pairs) of the users object, and use .find to find the username that matches the one you're trying to find. The first item in the entry (the key) will be what you're looking for:

const obj = {
  "users": {
    "2211392761": {
      "username": "user1"
    },
    "14300995184": {
      "username": "user2"
    },
    "2781554712": {
      "username": "user3"
    },
    "3554341": {
      "username": "user4"
    },
    "202611": {
      "username": "user5"
    },
    "17754300653761": {
      "username": "user6"
    }
  }
}
const findEntry = usernameToFind => {
  const foundEntry = Object.entries(obj.users)
    .find(([, { username }]) => username === usernameToFind);
  if (foundEntry) return foundEntry[0];
};
console.log(findEntry('user5'));
console.log(findEntry('userthatdoesntexist'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.