繁体   English   中英

在adonis中反序列化json

[英]Deserialize json in adonis

我有这两个对象数组:

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
  }
]

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
  }
]

我通过book_idbookUnitIdInfomacoes分组为key => value对的对象作为book_id => array of books然后,使用 [ Object.values ] 仅检索此groupedObj对象的值。 最后,我们映射idiomas并将相应的书组添加到名为quizz的新属性中。 对于给定的idioma ,我们知道它的位置idiomas感谢的第二个参数mapi 我们简单地做grouped[i]来获得相应的书籍组。

编码:

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]

 }));

但是当我做一个返回结果时,我有这个:

[
    {
        "__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
            }
        ]
    }
]

我如何反序列化这个? JSON.stringify(result)返回相同的 json。

尝试这个:

const result = idiomas.map(({ $attributes }, i) => ({

  ...$attributes,

  quizz: grouped[i]

}))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM