0

I want to make json object of html table in javascript.Currently I am able to read the each cells value in javascript, the only problem is that I am not able to retrieve as per my need so think of any suggestion here. Currently getting value like:

var x = document.getElementById("tableId");

for (var i = 0; i < x.rows.length; i++) {
    for (var j = 0; j < x.rows[i].cells.length; j++){
            tableJsonDTO[name] = x.rows[i].cells[j].innerHTML;
    }
}

This is how i am able to read the each cell value.The table format is as follow:

header:      company_1      company_2      company_3

Question1    answer_1       answer_1       answer_1

Question2     answer_2       answer_2       answer_2

and so on.How can i read the value so that i can get object like:

var obj = [{spname:"company_1",qalist:[{question:"",answer:""},{question:"",answer:""}]},{spname:"company_2",qalist:[{question:"",answer:""},{question:"",answer:""}]},{spname:"company_3",qalist:[{question:"",answer:""},{question:"",answer:""}]}]

Please give some suggestion.

4
  • Hope the below site helps you. stackoverflow.com/questions/16507222/… Commented Aug 11, 2014 at 6:32
  • sorry there the situation is that the rows are different. Commented Aug 11, 2014 at 6:38
  • you have to transpose the table first stackoverflow.com/questions/2730699/… link to JSFIDDLE Commented Aug 11, 2014 at 6:52
  • as per design concert we cant transpose table but for json object it is ok, so on jsfiddle how to get json object as shown in question. Commented Aug 11, 2014 at 7:08

1 Answer 1

0

You simply need to change the way you put values to tableJsonDTO. Demo.

document.getElementById('toJSON').addEventListener('click', function(){
    var table = document.getElementById('mytable'),
        names = [].slice.call(table.rows[0].cells), 
        values = [].slice.call(table.rows, 1),
        out = {};


    values.forEach(function(row) { //iterate over values
        var cells = row.cells,
            caption = cells[0].innerHTML; //get property name

        [].forEach.call(cells, function(cell, idx) { //iterate over answers
            var value = {}, 
                arr;
            if(!idx) return; //ignore first column

            value[caption] = cell.innerHTML; //prepare value

            //get or init array of values for each name
            arr = out[names[idx].innerHTML] || (out[names[idx].innerHTML] = []);

            arr.push(value); 
        });
    });

    console.log(out);
});
Sign up to request clarification or add additional context in comments.

8 Comments

sorry but I am trying to iterate object it is giving undefined error.
@user3406754 hmm which line?
sorry but instead of var obj = {company_1: [{Question1: answer_1},{Question2: answer_2}],company_2[{Question1: answer_1},{Question_2:answer_2}]}; can I get the var obj = {[name:company_1,[{Question1: answer_1},{Question2: answer_2}]],[name:company_2,[{Question1: answer_1},{Question2: answer_2}]]}
please suggest something.
@user3406754 you can't have structure like this var obj = {[name:company_1,[{Question1: answer_1},{Question2: answer_2}]],[name:company_2,[{Question1: answer_1},{Question2: answer_2}]]} syntax is invalid.
|

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.