0

I'm using PAPA parser to parse a .csv file and the file looks like this in Chrome debug tools.

       [Object]
data: Array[4]
    0: Array[5]
    0: "title"
    1: "summary_text"
    2: "date"
    3: "type"
    4: "link"
    length: 5
    __proto__: Array[0]
    1: Array[5]
    0: "Choice, Happiness and Spaghetti Sauce"
    1: "Delivering product variety: chunky spaghetti sauce, zesty pickles, or weak coffee? Gladwell explains how the root of happy customers is product variety."
    2: "30-Jan-13"
    3: "sm"
    4: "https://www.facebook.com/permalink.php?id=380939405308665&story_fbid=298838986905013"
    length: 5
    __proto__: Array[0]
    2: Array[5]
    3: Array[1]
    length: 4
    __proto__: Array[0]
    errors: Array[0]
    meta: Object
    __proto__: Object
    length: 1
    __proto__: jQuery[0]

With this I want to be able to extract the title, link, date and type and put all this into an <li> tag. How can I achieve this? I was able to parse the data and I get this and I am able to paste all the data into the HTML by using JSON.stringify(r2, null, 4);.

I want to loop through the data parent and get all the child title, dates, descriptions, type, etc.. and add these to an li tag.

Would it look something like right?

Papa.parse("http://support.jonar.com/support/default.asp?pg=pgDownload&pgType=pgWikiAttachment&ixAttachment=142896&sFileName=newsroom.csv", {
    download: true,
    complete: function(results) {
            //console.log("REMOTE FILE PARSED!", results.data);
                var r2 = $(results);
                //var r3 = JSON.stringify(r2, null, 4);
                //console.log(r2)
                $('ul.nflist').append( $('<li />', {text: results}));
    }
});
1

1 Answer 1

2
function(results) {
    $.each(results.data.slice(1), // skip first row of CSV headings
        function(i, 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> ' + date + ' ' + type
            });
    });
}
Sign up to request clarification or add additional context in comments.

20 Comments

Use if (type == 'sm') around the call $('ul.nflist').append().
This is beginner programming if (type == 'sm') { $('ul.nflist').append(...); }
The same way you do anything else depending on the value of a variable.
You're removing the second one with this line: $('ul.nflist li:last-child').remove();
Because data is an array, not an object.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.