【问题标题】:Filter parsed csv data with Jquery使用 Jquery 过滤解析的 csv 数据
【发布时间】:2015-09-16 19:36:23
【问题描述】:

我正在使用此代码指定要获取的数据以及如何将其添加到我的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 它不应该包含行..我如何过滤数据?

【问题讨论】:

  • 一般来说,results.data.slice(1).filter(function(o){return o.type!="sm"; })
  • 嘿@dandavis,不幸的是没有工作..这是我的问题,它应该很简单,但我无法弄清楚

标签: javascript jquery html json csv


【解决方案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;
               }
              }
        );

【讨论】:

    【解决方案2】:

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

    改变:

     var title = data[0];
    

    收件人:

     var title = data.title;
    

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

    【讨论】:

    • 最后一行没有完成,所以最好先检查所有这些属性是否存在...假设显示的数据是准确的
    • 嗨@charliefl 最后一行包含一个空格,这是我将删除的唯一问题...尽管将其更改为 .title 会使我的数据未定义
    • 根据显示的内容应该可以正常工作...这是一个简单的演示plnkr.co/edit/pG4D9uaV2D4CZEWIAXtT?p=preview
    • 嗨@charliefl 我刚刚看了你的网址它不起作用它给每一行...sm只有2行...
    • 我复制if 太快了...现在只为sm 工作。重点是不应该有undefined
    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    相关资源
    最近更新 更多