Could the repetitive keys be displayed as one record and a single-man data set be assigned to it



  • Models

    const Quest = sequelize.define('quest' , {
     name: {
       type: DataTypes.STRING,
       allowNull: false,
     }
    });
    const QuestPack = sequelize.define('quest_packs', {
     day: {
       type: DataTypes.INTEGER,
       allowNull: false,
     },
     questId: {
       type: DataTypes.INTEGER,
       allowNull: false,
     },
     videoId: {
       type: DataTypes.INTEGER,
       allowNull: false,
     }
    });
    

    const Video = sequelize.define('video', {
    url: {
    type: DataTypes.INTEGER,
    allowNull: false,
    }
    });
    Quest.belongsToMany(Video, {
    through: QuestPack,
    });
    Quest.hasMany(QuestPack, {
    foreignKey: 'challengeId',
    });
    QuestPack.belongsTo(Video, {
    foreignKey: 'videoId',
    });
    Video.belongsToMany(Quest, {
    foreignKey: 'videoId',
    through: QuestPack,
    });

    Request

    const data = await Quest.findByPk(1, {
    include: [
    {
    model: QuestPack,
    attributes: ['day'],
    include: [{ model: Video }],
    }
    ],
    });

    I get data in this format:

    {
    "id": 1,
    "name": "Name quest",
    "questPacks": [
    {
    "day": 1,
    "video": {
    "id": 1,
    "url": "youtube link",
    }
    },
    {
    "day": 1,
    "video": {
    "id": 2,
    "url": "youtube link",
    }
    }
    ]
    }

    I want all my repetitive records on the key. day were removed and only one recording containing all the questId videos was displayed.

    {
    "id": 1,
    "name": "Name quest",
    "questPacks": [
    {
    "day": 1,
    "videos": [
    {
    "id": 1,
    "url": "youtube link",
    },
    {
    "id": 2,
    "url": "youtube link",
    }
    ]
    },
    ]
    }

    Can this be implemented from sequelize?



  • We're building a new questPacks, then by searching for matches by day, adding the other videos.

    const o = {
      "id": 1,
      "name": "Name quest",
      "questPacks": [
        {
          "day": 1,
          "video": {
            "id": 1,
            "url": "youtube link",
          }
        },
        {
          "day": 1,
          "video": {
            "id": 2,
            "url": "youtube link",
          }
        },
        {
          "day": 2,
          "video": {
            "id": 2,
            "url": "youtube link",
          }
        },
        {
          "day": 1,
          "video": {
             "id": 3,
             "url": "youtube link",
        }
    },
      ]
    }
    

    function packDays(packs) {
    const result = {
    id: packs.id,
    name: packs.name,
    questPacks: []
    }

    result.questPacks = o.questPacks.reduce((acc, pack) => {
    let nextPack = null;
    if (nextPack = o.questPacks.find(days => days.day === pack.day) ){
    // первый объект (видео по дню) помещаем в массив
    if(nextPack === pack) {
    nextPack.video = [nextPack.video]
    acc.push(pack)
    return acc
    }
    // при совпадении дней пушим их в один массив
    nextPack.video.push(pack.video);
    return acc
    }
    }, [/* инициируем пустой массив */])
    return result
    }

    const inp = document.querySelector('#inp');
    const out = document.querySelector('#out');
    inp.textContent = JSON.stringify(o, null, 2)
    out.textContent = JSON.stringify(packDays(o), null, 2)

    <p><b>оригинал:</b></p>
    <pre id="inp"></pre>
    <p><b>получаем:</b></p>
    <pre id="out"></pre>



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2