解析JSON以排除重复的Node.JS [英] Parsing JSON to exclude duplicates Node.JS

查看:194
本文介绍了解析JSON以排除重复的Node.JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是Node.js和JavaScript的新手,所以任何帮助将非常感谢。

First off, I'm new to NodeJS and JavaScript, so any help would be greatly appreciated.

我的目标是能够解析csv文件我可以看到有多少用户每个月登录。为了我这样做,我使用npmCSVtoJSON将csv文件转换为JSON格式。这是我从JSON文件,但超过8000多个条目。以下是一段代码。

My goal is to be able to parse a csv file so that I can see how many users log in each month. In order for me to do that, I've used the npm "CSVtoJSON" to convert the csv file to JSON format. This what I've get from the JSON file but with over 8000+ entries. 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'}
]

我的代码:

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

我试图找出解析JSON的最佳方法,的同名的数组。如果我做这个完全错误,请指出我的方向正确。谢谢你们的帮助。

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. 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!

推荐答案

使用独特的

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]);
        } 
    }
}

em>过滤器如果你发现它更容易理解,在这个特定的例子它不是很干净。一个非常大的数组 也可能使得过滤器的开销成本过高。

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

这篇关于解析JSON以排除重复的Node.JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆