0

I have an array within my JSON file which looks as follows:

{
 "commands": [
   {
     "user": "Rusty",
     "user_id": "83738373",
     "command_name": "TestCommand",
     "command_reply": "TestReply"
   }
 ] 
}

and so on. I want to limit the amount of commands to a certain user (recognized by the user_id) to 3 commands. I know I need to start by looping through each object but stuck at how to accomplish this part.

  • So you count and ignore? – epascarello Apr 18 '17 at 19:49
  • 2
    Your title and question text don't really match. Getting a count is covered by http://stackoverflow.com/q/17615364/215552. If you want to get the commands with a specific userId, that's covered by http://stackoverflow.com/q/13964155/215552. – Heretic Monkey Apr 18 '17 at 19:53
  • @MikeMcCaughan I would agree with you that this question can be answered by the 2 posts you linked, but I wouldn't say the title and question don't match. As per the title, the OP wants to count the number of times a specific value occurs for a specific property on an array of objects. I don't think anything in the body of the question indicates otherwise. – mhodges Apr 18 '17 at 20:09
  • 1
    @mhodges "I want to limit the amount of commands ... to 3 commands." alludes to other requirements which we are not privy to, and which should affect the final answer. For instance, perhaps the OP wants to get the first 3 commands and ignore the rest, as suggested by epascarello in the first comment. Maybe they want to group by user_id and get the counts, which you've demonstrated (and is demonstrated in many other answers across the site). Basically, we don't know because the OP has not responded. – Heretic Monkey Apr 18 '17 at 20:15
  • @MikeMcCaughan Ah, true. I did not read it that way, but I can totally see what you're saying now. – mhodges Apr 18 '17 at 20:25

1 Answers1

3

You can do this by using the .reduce() method on the Array prototype. The idea is to go through the commands array and generate a key/value pair of the userIds and the number of commands executed by that user. The structure would look like the following:

{"83738373": 3, "83738334": 2}

You can then check against the userCommandCounts to determine whether a user can execute another command or not.

var data = {
  "commands": [{
      "user": "Rusty",
      "user_id": "83738373",
      "command_name": "TestCommand",
      "command_reply": "TestReply"
    },
    {
      "user": "Jill",
      "user_id": "83738334",
      "command_name": "TestCommand",
      "command_reply": "TestReply"
    },
    {
      "user": "Rusty",
      "user_id": "83738373",
      "command_name": "TestCommand",
      "command_reply": "TestReply"
    },
    {
      "user": "Rusty",
      "user_id": "83738373",
      "command_name": "TestCommand",
      "command_reply": "TestReply"
    },
    {
      "user": "Jill",
      "user_id": "83738334",
      "command_name": "TestCommand",
      "command_reply": "TestReply"
    },
  ]
};

var userCommandCounts = data.commands.reduce(function (result, current) {
  if (!result[current["user_id"]]) {
    result[current["user_id"]] = 0;
  }
  result[current["user_id"]]++;
  return result;
}, {});

function canUserExecute (userId) {
  return !userCommandCounts[userId] || userCommandCounts[userId] < 3; 
}

console.log(canUserExecute("83738373"));
console.log(canUserExecute("83738334"));
console.log(canUserExecute("23412342"));
mhodges
  • 10,938
  • 2
  • 28
  • 46