ToMap to export the datatable to a json-like struct
(opt ...ExportOption)
| 26 | |
| 27 | // ToMap to export the datatable to a json-like struct |
| 28 | func (t *DataTable) ToMap(opt ...ExportOption) []map[string]interface{} { |
| 29 | if t == nil { |
| 30 | return nil |
| 31 | } |
| 32 | |
| 33 | opts := newExportOptions(opt...) |
| 34 | if err := t.evaluateExpressions(); err != nil { |
| 35 | panic(err) |
| 36 | } |
| 37 | |
| 38 | // visible columns |
| 39 | cols := make(map[string]int) |
| 40 | for i, col := range t.cols { |
| 41 | if opts.WithHiddenCols || col.IsVisible() { |
| 42 | cols[col.Name()] = i |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | rows := make([]map[string]interface{}, 0, t.nrows) |
| 47 | for i := 0; i < t.nrows; i++ { |
| 48 | r := make(map[string]interface{}, len(cols)) |
| 49 | for name, pos := range cols { |
| 50 | r[name] = t.cols[pos].serie.Get(i) |
| 51 | } |
| 52 | rows = append(rows, r) |
| 53 | } |
| 54 | return rows |
| 55 | } |
| 56 | |
| 57 | // ToTable to export the datatable to a csv-like struct |
| 58 | func (t *DataTable) ToTable(opt ...ExportOption) [][]interface{} { |