计算某个键的值在JSON中出现的次数

时间:2022-02-25 23:58:48

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

我的JSON文件中有一个数组,如下所示:

{
 "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.

等等。我想限制某个用户(由user_id识别)的命令数量为3个命令。我知道我需要从遍历每个对象开始,但坚持如何完成这一部分。

1 个解决方案

#1


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:

您可以通过在Array原型上使用.reduce()方法来完成此操作。我们的想法是通过命令数组并生成userIds的键/值对以及该用户执行的命令数。结构如下所示:

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

{“83738373”:3,“83738334”:2}

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

然后,您可以检查userCommandCounts以确定用户是否可以执行另一个命令。

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"));

#1


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:

您可以通过在Array原型上使用.reduce()方法来完成此操作。我们的想法是通过命令数组并生成userIds的键/值对以及该用户执行的命令数。结构如下所示:

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

{“83738373”:3,“83738334”:2}

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

然后,您可以检查userCommandCounts以确定用户是否可以执行另一个命令。

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"));