SQLite Forum

Merge Data
Login
Hello,

is it possible to merge data into array?

for example i have two result from two different query (i can also ava one query with left join)

{
  "columns": [
    {
      "id": 1,
      "columntitle": "First column",
      "state": "active"
    },
    {
      "id": 2,
      "columntitle": "Second column,
      "state": "active"
    }
  ],
  "tile": [
    {
      "tiletitle": "First tile",
      "autore": "John ",
      "idcolumn": "1",
      "message": "mymessage"
    }
  ]
}

i would like it to be :

{
  "columns": [
    {
      "id": 1,
      "columntitle": "First column",
      "state": "active"
    "tile": [
    {
      "tiletitle": "First tile",
      "autore": "John ",
      "idcolumn": "1",
      "message": "mymessage"
    }
  ]
    },
    {
      "id": 2,
      "columntitle": "Second column,
      "state": "active"
      "tile": []
    }
  ]
}

Here is my code:

exports.columnandtile = async (req, res) => {

const columns = await knex('column')
  
const tile = await knex('tile').whereIn('idcolumn', columns.map(column => column.id));

res.json({columns:columns,tile:tile})
}

Thank you!