DanielM96
DanielM96

Reputation: 103

Converting a jquery array into a vue array

I have a jquery function that converts an table row into an array.

 $("#table1 tr").click(function () {
                // alert($(this).text());
                //  alert($(this).children("td").html());
                // app.greet();
                var $row = $(this).closest("tr"),       // Finds the closest row <tr> 
                    $tds = $row.find("td");             // Finds all children <td> elements

                $.each($tds, function () {               // Visits every single <td> element
                    // console.log($(this).text());
                    list.push($tds);
                    // Prints out the text within the <td>
                });
                console.log(list.length)
            });

I want to then convert the jquery array into my vue array which is structured this way.

 data() {
        return {
            tableRow: [
                {
                    "name": "name1",
                    "numSongs": "joker",
                    "Year": "year"
                }
            ]
        }
    },

This is the way i was trying to do it but it im getting a unexpected end of json input error.

 this.tableRow = JSON.parse(list);

Does anyone have any suggestions.

Upvotes: 0

Views: 113

Answers (1)

mateusz-dot
mateusz-dot

Reputation: 186

Json.Parse converts string to object, so you can't use it, try this:

       $("#table1 tr").click(function () {
            var list = [];

            var $row = $(this).closest("tr"),    
                $tds = $row.find("td");     

            list.push({name:$tds.eq(0).text(), numSongs:$tds.eq(1).text(), Year:$tds.eq(2).text()});
        });

You can change $tds.eq(index).text() to retrieve cell value.

Upvotes: 1

Related Questions