peace_love
peace_love

Reputation: 6461

How can I split a comma separated variable?

I have a function that gives me some data output:

$(document).ready(function () {
    var table = $('#myTable').DataTable();
    table.MakeCellsEditable({
        "onUpdate": myCallbackFunction
    });
});

function myCallbackFunction(updatedCell, updatedRow, oldValue,row) {
     console.log("The values for each cell in that row are: " + updatedRow.data());
}

My console output is: The values for each cell in that row are: 2,Alan,Dirt,[email protected]

I want now to output inside my function only the first value of the comma separated list. So I changed it to the following:

 function myCallbackFunction(updatedCell, updatedRow, oldValue,row) {
       var row = updatedRow.data();
       var id = row.split(","); 
         console.log("The first value is: " + id[0]);
    }

But I get the console error now TypeError: row.split is not a function. (In 'row.split(",")', 'row.split' is undefined)

What does this mean?

Upvotes: 0

Views: 64

Answers (2)

Ali Yousuf
Ali Yousuf

Reputation: 702

The updatedRow.data(); must already be an array.

Just use the following:

function myCallbackFunction(updatedCell, updatedRow, oldValue,row) {
   var row = updatedRow.data(); // this is array already
   console.log("The first value is: " + row[0]);
}

How I'm sure that row is an Array? If you do the following,

var data = ["my", "world"];
console.log("Hello " + data);

the output will be Hello my,world. The same as in your example.

So you can directly use data[0] as it's already an array.

Upvotes: 1

Ahmad Ibrahim
Ahmad Ibrahim

Reputation: 197

You need to convert the data to string like this

var row = updatedRow.data().toString();

Upvotes: 1

Related Questions