0
votes

I have two json files that I would like to combine into a single json object that can be read on my front end.

fragments.json

[
    {
      "id": 2,
      "title": "John Smith is nice",
      "reports": "1,2",
      "entities": 1,
    }
]

entities.json

[
    {
      "id": 1,
      "fragments": 2,
      "name": "John SMITH",
      "dateOfBirth": "4/24/1967",
    }
]

Desired Result.json

I want to show all the entities that are connected to any given fragment.

[
    {
       "id":2,
       "title":"John Smith is nice",
       "reports":"1,2",
       "entities":{
          "id":1,
          "fragments":2,
          "name":"John SMITH",
          "dateOfBirth":"4/24/1967"
       }
    }
 ]

Here's my code so far:

router.get('/sprint-3/:prototype/report/:reportId', function (req, res) {

    const slug = +req.params.reportId
    const report = reports.find(({ id }) => id === slug);

    const reportFragments = fragments.filter(({ reports }) => reports.includes(report.id) );

    reportFragments.forEach(fragment => {
        const reportEntities = entities.filter(({ fragments }) => fragments === fragment.id );
        reportFragments.push(reportEntities);
        console.log('New', reportFragments)
    });


    res.render("sprint-3/prototype-1/report", { report: report, fragments: reportFragments });
});

I've been looking at .push as a way of doing this, but currently it appends the entities to the end of the reportFragments rather than nesting inside each of them.

unclear what connects the json records - OrenIshShalom