Can toArray() be used on .row().data()?

Can toArray() be used on .row().data()?

Eagle-ManEagle-Man Posts: 2Questions: 1Answers: 0

In my project, I want to get a row of data from the table as an array. In the table, it is an object. I see that there is a .toArray() function and that the example is used on .column(0).data(), but when I try the following, I get an error:

currentData[j] = table.row('#'+changes[i][0]).data().toArray();

This results in "Uncaught TypeError: table.row(...).data(...).toArray is not a function". Am I doing something wrong, or can toArray() not be used on a row?

(note: changes[i][0] contains the row id of the row I want to get)

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,275Questions: 26Answers: 4,765
    edited July 2019 Answer ✓

    Using row().data() returns the row as an object or array depending on what the Datatable was populated with. The toArray() API is used with API's that return API objects like rows().api(). It will take the API object and return a Javascript array of the row data (either an array of objects or an array of arrays depending on what populated the Datatable.

    I want to get a row of data from the table as an array. In the table, it is an object.

    toArray() won't convert object based data to an array. You will need to code this yourself or use something like Object.values().

    Kevin

  • Eagle-ManEagle-Man Posts: 2Questions: 1Answers: 0

    Ah. Thanks. I misread the row().data() page and though it also returned an API for some reason. Maybe I was accidentally looking at rows().data().

    Anyway, I already have a solution with a $.map, but I was wondering if I could simplify it. Apparently not that way. Thanks again.

This discussion has been closed.