使用Jquery过滤解析的csv数据

时间:2015-09-16 19:36:23

标签: javascript jquery html json csv

我正在使用此代码指定要采取的数据以及如何将其添加到HTML

window.onload = (function(CMS) {
//parse your csv file
Papa.parse("LINK", {
    download: true,
    complete: function(results) {
      console.log(results);
        $.each(results.data.slice(1), // skip first row of CSV headings
            function(find, data) {
                var title = data[0];
                var link = data[4];
                var date = data[2];
                var type = data[3];
                $('ul.nflist').append($('<li>', {
                    html: '<a href="' + link + '">' + title + '</a> ' + ' ' + '<span class="category">' + type + '</span>'
                }));
            });
        //weird issue with an empty line.. just delete the last <li>
        $('ul.nflist li:last-child').remove();
        //Now that your data is clean and in its proper place you can begin to do the preety stuff like add images to corresponding categories
        //First hide the category tag...
        $(".category").hide();
        // Now lets search for categories and add images
        $('ul.nflist li:contains("sm")').prepend('<img src="http://www.jonar.com/portal/partner/img/sendemailo.png" />');
        $('ul.nflist li:contains("blog")').prepend('<img src="http://www.jonar.com/portal/partner/img/bello.png" />');
        $('ul.nflist li:contains("ann")').prepend('<img src="http://www.jonar.com/portal/partner/img/calendaro.png" />');
        $('ul.nflist li:contains("itp")').prepend('<img src="http://www.jonar.com/portal/partner/img/pluscircleo.png" />');
    }
});

});

解析csv时,json文件看起来像这样:

[
{
    "title": "Choice, Happiness and Spaghetti Sauce",
    "summary_text": "Delivering product variety: chunky spaghetti sauce, zesty pickles, or weak coffee? Gladwell explains how the root of happy customers is product variety.",
    "date": "30-Jan-13",
    "type": "sm",
    "link": "https://www.facebook.com/permalink.php?id=380939405308665&story_fbid=298838986905013"
},
{
    "title": "Servoy Interviewed Us",
    "summary_text": "Servoy interviewed our managing director,Jon Ruby. Find out why we chose Servoy and learn about our development best practices.",
    "date": "4-Nov-14",
    "type": "itp",
    "link": "https://plus.google.com/+ServoyPlus/posts/aUL28cD2hfH"
},
{
    "title": "Come Together, Right Now: The Benefits of Cross-Functional Teams",
    "summary_text": "What do you get when you put an engineer, a doctor and a lawyer in one room? Find out how to create an environment to sustain cross-functional teams.",
    "date": "2-May-13",
    "type": "blog",
    "link": "http://www.jonar.com/jonarblog/come-together-right-now-the-benefits-of-cross-functional-teams/"
},
{
    "title": "What is ERP?",
    "summary_text": "The acronym, �ERP� is tossed around constantly, but what does it actually mean for your business?",
    "date": "25-Aug-11",
    "type": "sm",
    "link": "https://www.youtube.com/watch?v=PVRgIXLWDHs"
},
{
    "title": "Admit You Don�t Know � Take The Road Less Traveled",
    "summary_text": "If you�ve ever felt like a fraud, you�re not alone. Instead of ignoring it, here�s why you should embrace the experience of Imposter Syndrome.",
    "date": "17-Oct-14",
    "type": "blog",
    "link": "http://www.jonar.com/jonarblog/admit-you-dont-know-take-the-road-less-travelled/"
},
{
    "title": "Jonar is Looking to Build Our Network of VAP�s",
    "summary_text": "Jonar has started building an international network of partners. Want in? Join us on our journey to reinvent ERP.",
    "date": "no date",
    "type": "ann",
    "link": "http://www.jonar.com/new/signUp.html"
},
{
    "title": ""
}

以下是有sm种类型的问题。我不想要这些行...让我们说类型sm它不应该包含行..如何过滤数据?

2 个答案:

答案 0 :(得分:1)

使用此

       $.each(results.data.slice(1), // skip first row of CSV headings
              function(find, data) {
               if(data.type !="sm") { 
                   var title = data.title;
                   var link  = data.link;
                   var date  = data.date;
                   var type  = data.type;
               }
              }
        );

答案 1 :(得分:0)

数组的每个元素都是一个对象,因此您需要访问该对象的属性:

更改:

 var title = data[0];

要:

 var title = data.title;

对所有其他变量和属性执行相同操作