简体   繁体   English

解析JSON以排除重复的Node.JS

[英]Parsing JSON to exclude duplicates Node.JS

First off, I'm new to NodeJS and JavaScript, so any help would be greatly appreciated. 首先,我是NodeJS和JavaScript的新手,因此将非常感谢您的帮助。

My goal is to be able to parse a csv file so that I can see how many users log in each month. 我的目标是能够解析一个csv文件,以便可以查看每月有多少用户登录。 In order for me to do that, I've used the npm "CSVtoJSON" to convert the csv file to JSON format. 为了做到这一点,我使用了npm“ CSVtoJSON”将csv文件转换为JSON格式。 This what I've get from the JSON file but with over 8000+ entries. 这是我从JSON文件中获得的信息,但有8000多个条目。 Below is a snippet. 下面是一个片段。

[
 { date: '2015-04-09T19:30:22.213795+00:00',
   'email address': '',
   'full name': 'Canton Bashkin',
   action: 'LoggedInActivity',
   application: '',
   project: '',
   task: '',
   'other email address': '',
   'other full name': '',
   description: 'Canton Bashkin logged in' },
 { date: '2015-04-08T00:34:42.261728+00:00',
   'email address': '',
   'full name': 'Sha Phad',
   action: 'LoggedInActivity',
   application: '',
   project: '',
   task: '',
   'other email address': '',
   'other full name': '',
   description: 'Sha Phad logged in' },
 { date: '2015-04-07T23:31:32.559654+00:00',
   'email address': '',
   'full name': 'Canton Bashkin',
   action: 'LoggedInActivity',
   application: '',
   project: '',
   task: '',
   'other email address': '',
   'other full name': '',
   description: 'Canton Bashkin logged in' },
 { date: '2015-04-07T23:31:02.408628+00:00',
   'email address': '',
   'full name': 'Sha Phad',
   action: 'LoggedInActivity',
   application: '',
   project: '',
   task: '',
   'other email address': '',
   'other full name': '',
   description: 'Sneha Phadke logged in'}
]

My code: 我的代码:

//Converter Class 
var Converter = require("csvtojson").Converter;
var converter = new Converter({});
var allUsers = [];

//end_parsed will be emitted once parsing finished
function parseUsers() {

    converter.on("end_parsed", function (jsonArray) {
        for (var i = 0; i < jsonArray.length; i++) {
            var users = jsonArray[i]['full name'];
            if (jsonArray[i]['action'] === 'LoggedInActivity') {
                if (users != 'SD Elements Support' && users != 'Karan Sharala' && users != 'Rick Bogans'
                && users != 'Kanal Bhatya' && users != 'Sanka Saja' && users != 'Kiiko Plash') {
                        allUsers.push(jsonArray[i]);    
                } 
            }
        }

        console.log(allUsers);

    });
}

parseUsers();


//read from file 
require("fs").createReadStream("log.csv").pipe(converter);

I am stuck trying to figure out the best way to parse JSON so that I don't add duplicates of the same name into the array. 我一直在努力找出解析JSON的最佳方法,这样就不会在数组中添加相同名称的重复项。 If I'm doing this completely wrong, please point me in the right direction. 如果我完全错了,请指出正确的方向。 Thank you guys for the help. 谢谢你们的帮助。

After I get the names, I need to separate them into individual months, and I'm not too sure how to do that. 得到名字后,我需要将它们分成几个月,我不太确定该怎么做。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Using an Object called unique to keep track of already added Strings 使用称为unique对象来跟踪已添加的字符串

var unique = Object.create(null); // empty object, no inheritance
for (var i = 0; i < jsonArray.length; i++) {
    var users = jsonArray[i]['full name'];
    if (jsonArray[i]['action'] === 'LoggedInActivity') {
        if (!(users in unique)) { // not seen this `full name` before
            unique[users] = allUsers.push(jsonArray[i]);
        } 
    }
}

You can combine this with filter if you find it easier to understand, in this particular example it is not much cleaner. 如果您觉得它更容易理解,可以将其与过滤器结合使用,在此特定示例中,它并不太干净。 A very large Array may also make the cost of function overhead with filter too expensive. 很大的阵列也可能会使使用过滤器的功能开销的成本过于昂贵。

var unique = Object.create(null);
allUsers = jsonArray.filter(function (e) {
    if (e['action'] === 'LoggedInActivity' && !(e['full name'] in unique)) {
        return unique[e['full name']] = true;
    }
    return false;
});

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

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