0

I am tring to convert the html table data into something like this: jsFiddle Demo

[{
    name: 'Week 1',
    data: [33.95, 40.68]},
{
    name: 'Week 2',
    data: [11.99, 16.66]},
{
    name: 'Week 3',
    data: [1.96, 0.93]},
{
    name: 'Week 4',
    data: [0, 1.21]}]

Method I am using:

var Consolelog = $('table tbody tr').map(function() {

    var $row = $(this);
    return {
        name: $row.find(':nth-child(1)').text(),
        data: $('td:not(:nth-child(1))', this).each( function(){

            $(this).text();

        })
    };


}).get();

console.log(Consolelog);
alert(Consolelog.toSource());

But, this give me something like this:

[{
    name: "Some Data 1 ",
    data: {
        length: 4,
        prevObject: {
            0: ({}),
            context: ({}),
            length: 1
        },
        context: ({}),
        selector: "td:not(:nth-child(1))",
        0: ({}),
        1: ({}),
        2: ({}),
        3: ({})
    }},
{
    name: "Some Data 1 ",
    data: {
        length: 4,
        prevObject: {
            0: ({}),
            context: ({}),
            length: 1
        },
        context: ({}),
        selector: "td:not(:nth-child(1))",
        0: ({}),
        1: ({}),
        2: ({}),
        3: ({})
    }}]​

Any suggestion what I am doing wrong?

1
  • You are doing it inversely. You set a name to first cell in a row, not first cell in a column. Commented Jul 6, 2012 at 11:41

1 Answer 1

1

sorry but i've not much time to test, but this could be a way to do that:

var Consolelog = $('table th:not(:nth-child(1))').map(function(i) {

    var $th = $(this);
    var $cel = $('td:nth-child('+(i+2)+')').each(function(){
                       $(this);
                });


    return {
        name: $th.text(),
        data: $cel.text().split('%').slice(0,-1)
    };
    //console.log(return);

}).get();

//console.log(Consolelog);
alert(Consolelog.toSource());

See update Fiddle

UPDATE:

Hi, sorry for the delay, by the way to convert or to be sure that array value are number you could try this:

var Consolelog = $('table th:not(:nth-child(1))').map(function(i) {

    var $th = $(this);
    /*var $cel = $('td:nth-child('+(i+2)+')').each(function(){
                       $(this);
                });*/
    var $cel = $.map(
        $('td:nth-child('+(i+2)+')').each(function(){
                       $(this);
        }).text().split('%').slice(0,-1), function(value){
            return parseFloat(value);
        });

    return {
        name: $th.text(),
        //data: $cel.text().split('%').slice(0,-1)
        data: $cel
    };
    //console.log(return);

}).get();

//console.log(Consolelog);
alert(Consolelog.toSource());

See example Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Great. 99% there. Is there any way to make sure data array is numbers / values and not a strings?
@NewUser Hi, sorry for the delay but I was away from home, however I have updated the answer, I hope goes well

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.