How i can deserialize this?

Hi, i have this two arrays of objects:

bookUnitIdInformacoes

[
  {
    "id": 5,
    "book_id": 33,
    "unit": 1,
    "sequence": 1,
    "description": "UNIT_01_GRAMMAR",
    "qt_question": 5,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 08:11:21",
    "updated_at": "2019-12-30 14:54:12",
    "miniature": null
  },
  {
    "id": 6,
    "book_id": 33,
    "unit": 1,
    "sequence": 2,
    "description": "UNIT_01_VOCABULARY",
    "qt_question": 5,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 08:11:39",
    "updated_at": "2019-12-27 08:11:39",
    "miniature": null
  },
  {
    "id": 7,
    "book_id": 33,
    "unit": 2,
    "sequence": 1,
    "description": "UNIT_02_GRAMMAR",
    "qt_question": 5,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-27 08:11:46",
    "updated_at": "2019-12-27 08:11:46",
    "miniature": null
  },
  {
    "id": 8,
    "book_id": 39,
    "unit": 1,
    "sequence": 1,
    "description": "UNIT_01_GRAMMAR",
    "qt_question": 5,
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-30 11:07:09",
    "updated_at": "2019-12-30 15:03:50",
    "miniature": null
  }
]

and idioma:

[
  {
    "id": 13,
    "code": "ING-NOT-2019",
    "description": "Inglês Noturno 2019",
    "start_date": "2019-12-30T03:00:00.000Z",
    "end_date": "2019-12-31T03:00:00.000Z",
    "period": "Noturno",
    "language": "Inglês",
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-30 10:04:47",
    "updated_at": "2020-01-05 16:08:00",
    "language_substring": "US"
  },
  {
    "id": 14,
    "code": "ESP-MAN-2019",
    "description": "Espanhol manh√£ 2019",
    "start_date": "2019-12-30T03:00:00.000Z",
    "end_date": "2019-12-31T03:00:00.000Z",
    "period": "Manh√£",
    "language": "Espanhol",
    "status": false,
    "user_id": 1,
    "created_at": "2019-12-30 11:06:44",
    "updated_at": "2019-12-30 11:06:44",
    "language_substring": null
  }
]

I’m grouping the bookUnitIdInfomacoes by book_id reducing the array into an object of key => value pairs as book_id => array of books
Then, using [ Object.values ] to retrieve only the values of this groupedObj object.
Lastly, we map idiomas and add the corresponding book group into a new property called quizz . For a given idioma , we know its position in idiomas thanks to the second argument of map : i . We simply do grouped[i] to get the corresponding group of books.

The code:

const groupedObj = bookUnitIdInformacoes.reduce((grouped, info) => {

   grouped[info.book_id] = grouped[info.book_id] || [];

   grouped[info.book_id].push(info);

   return grouped;

 }, {});

 const grouped = Object.values(groupedObj);

 const result = idiomas.map((idioma, i) => ({

   ...idioma,

   quizz: grouped[i]

 }));

But when i do a return result i have this:

[
    {
        "__setters__": [
            "$attributes",
            "$persisted",
            "primaryKeyValue",
            "$originalAttributes",
            "$relations",
            "$sideLoaded",
            "$parent",
            "$frozen",
            "$visible",
            "$hidden"
        ],
        "$attributes": {
            "id": 2,
            "code": "ING-NOT-2019",
            "description": "Inglês Noturno 2019",
            "start_date": "2019-12-30T03:00:00.000Z",
            "end_date": "2019-12-31T03:00:00.000Z",
            "period": "Noturno",
            "language": "Inglês",
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-30T13:04:47.000Z",
            "updated_at": "2020-01-05T19:08:00.000Z",
            "language_substring": "US"
        },
        "$persisted": true,
        "$originalAttributes": {
            "id": 2,
            "code": "ING-NOT-2019",
            "description": "Inglês Noturno 2019",
            "start_date": "2019-12-30T03:00:00.000Z",
            "end_date": "2019-12-31T03:00:00.000Z",
            "period": "Noturno",
            "language": "Inglês",
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-30T13:04:47.000Z",
            "updated_at": "2020-01-05T19:08:00.000Z",
            "language_substring": "US"
        },
        "$relations": {},
        "$sideLoaded": {},
        "$parent": null,
        "$frozen": false,
        "quizz": [
            {
                "id": 1,
                "book_id": 1,
                "unit": 1,
                "sequence": 1,
                "description": "UNIT_01_GRAMMAR",
                "qt_question": 5,
                "status": false,
                "user_id": 1,
                "created_at": "2019-12-27 08:11:21",
                "updated_at": "2019-12-30 14:54:12",
                "miniature": null
            },
            {
                "id": 2,
                "book_id": 1,
                "unit": 1,
                "sequence": 2,
                "description": "UNIT_01_VOCABULARY",
                "qt_question": 5,
                "status": false,
                "user_id": 1,
                "created_at": "2019-12-27 08:11:39",
                "updated_at": "2019-12-27 08:11:39",
                "miniature": null
            },
            {
                "id": 3,
                "book_id": 1,
                "unit": 2,
                "sequence": 1,
                "description": "UNIT_02_GRAMMAR",
                "qt_question": 5,
                "status": false,
                "user_id": 1,
                "created_at": "2019-12-27 08:11:46",
                "updated_at": "2019-12-27 08:11:46",
                "miniature": null
            }
        ]
    },
    {
        "__setters__": [
            "$attributes",
            "$persisted",
            "primaryKeyValue",
            "$originalAttributes",
            "$relations",
            "$sideLoaded",
            "$parent",
            "$frozen",
            "$visible",
            "$hidden"
        ],
        "$attributes": {
            "id": 3,
            "code": "ESP-MAN-2019",
            "description": "Espanhol manh√£ 2019",
            "start_date": "2019-12-30T03:00:00.000Z",
            "end_date": "2019-12-31T03:00:00.000Z",
            "period": "Manh√£",
            "language": "Espanhol",
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-30T14:06:44.000Z",
            "updated_at": "2019-12-30T14:06:44.000Z",
            "language_substring": null
        },
        "$persisted": true,
        "$originalAttributes": {
            "id": 3,
            "code": "ESP-MAN-2019",
            "description": "Espanhol manh√£ 2019",
            "start_date": "2019-12-30T03:00:00.000Z",
            "end_date": "2019-12-31T03:00:00.000Z",
            "period": "Manh√£",
            "language": "Espanhol",
            "status": false,
            "user_id": 1,
            "created_at": "2019-12-30T14:06:44.000Z",
            "updated_at": "2019-12-30T14:06:44.000Z",
            "language_substring": null
        },
        "$relations": {},
        "$sideLoaded": {},
        "$parent": null,
        "$frozen": false,
        "quizz": [
            {
                "id": 4,
                "book_id": 2,
                "unit": 1,
                "sequence": 1,
                "description": "UNIT_01_GRAMMAR",
                "qt_question": 5,
                "status": false,
                "user_id": 1,
                "created_at": "2019-12-30 11:07:09",
                "updated_at": "2019-12-30 15:03:50",
                "miniature": null
            }
        ]
    }
]

How i can deserialize this? JSON.stringify(result) returns the same json.

1 Like

Did you mean to do result.toJSON() (not the same as JSON.stringify(result))?

https://adonisjs.com/docs/4.1/serializers