简体   繁体   English

如何遍历对象并计算键的唯一值?

[英]How to loop through objects and count unique values of key?

I have logs of json files which are objects that look like我有 json 文件的日志,这些文件看起来像

{
  "logs": [
    {
      "id": "12321321321321",
      "email": "test@email.com",
      "message": "ahahaha"
    },
    {
      "id": "12321321312",
      "email": "test@email.com",
      "message": "hahahaha."
    },
   "id": "12321321321"
}

I need to return a new object that contains我需要返回一个新的 object,其中包含

{
    "hello_id": outer id of the json file,
    "array": [
       
       {
        "email": "test@me.com",
        "total": 2
       }
      ]
    }

So far I am looping through the json files and have到目前为止,我正在遍历 json 文件并拥有

    jsonsInDirectory.forEach((file) => {
  const fileData = fs.readFileSync(path.join("./logs", file), "utf8");
  const jsonData = JSON.parse(fileData);
  }
});

The key is "logs" and "id" and the values are the objects in the "logs" and the value of "id"键是“logs”和“id”,值是“logs”中的对象和“id”的值

How can I count and return a new object at the same time?如何同时计算和返回一个新的 object?

You can try this approach: make a hash object that counts emails.您可以尝试这种方法:制作一个统计电子邮件的 hash object。 Then just map it to an array of objects.然后只需将 map 放到一个对象数组中。

 const data = { logs: [{ id: "89004ef9-e825-4547-a83a-c9e9429e8f95", email: "noah.sanchez@me.com", message: "successfully handled skipped operation." }, { id: "89004ef9-e825-4547-a83a-c9e9429e8f95", email: "noah.sanchez@me.com", message: "successfully handled skipped operation." }, { id: "89004ef9-e825-4547-a83a-c9e9429e8f95", email: "noname@me.com", message: "successfully handled skipped operation." }], id: "56f83bed-3705-4115-9067-73930cbecbc0", }; const emails = data.logs.reduce((acc, { email }) => { acc[email] = (acc[email]?? 0) + 1; return acc; }, {}); const tally = Object.entries(emails).map(([email, total]) => ({ email, total })); const result = { logs_id: data.id, tally }; console.log(result)
 .as-console-wrapper { max-height: 100%;important: top 0 }

When you do const jsonData = JSON.parse(fileData);当您执行const jsonData = JSON.parse(fileData); , you get the file data as a JSON and knowing the struct of that JSON you can easily get the info. ,您将文件数据作为 JSON 并知道该 JSON 的结构,您可以轻松获取信息。

I have created a example https://codesandbox.io/s/stackoverflow-logs-count-example-jys2vg?file=/src/index.js我创建了一个示例https://codesandbox.io/s/stackoverflow-logs-count-example-jys2vg?file=/src/index.js

It may not solve exactly wath you want.它可能无法完全解决您想要的问题。

To solve this problem with the most time efficiency, you can create a tally object by firstly creating a map of the occurences of each mail with the email as the key and no of occurences as the value, since this will take constant (O(1)) time to execute, afterwhich you can create the tally array from the map as given below (为了以最大的时间效率解决这个问题,您可以创建一个计数 object,方法是首先使用 email 的值创建每封邮件的出现次数的 map,因为此键将作为常量,并且)) 执行时间,之后您可以从 map 创建计数数组,如下所示

output = []
jsonsInDirectory.forEach((file) => {
  const fileData = fs.readFileSync(path.join("./logs", file), "utf8");
  const jsonData = JSON.parse(fileData);
  var map = {}
  jsonData.logs.forEach((log) => {
    if(log.email in map){
        map[log.email] += 1
    }
    else {
        map[log.email] = 1
    }
  });
  var tally = []
  for(var email in map){
    tally.push({email: email, total: map[email]})
  }
  output.push({logs_id: jsonData['id'], tally: tally});
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM