1

I have an API call to an JSON file which has some events. This events have one title but several showtimes. Is it possible to merge the nested array with the parent object and thus, creating n new events without nested arrays?

Events

{
    "publication_date": "8.1.2021 4:18",
    "title": "Camerata Salzburg",
    "showtime": [
        {
            "date_start": "2021-02-05",
            "location": "Konzerthaus"
        },
        {
            "location": "Konzerthaus",
            "date_start": "2021-02-07"
        }
    ],
}

How I would like my Events

{
    "publication_date": "8.1.2021 4:18",
    "title": "Camerata Salzburg",
    "date_start": "2021-02-05",
    "location": "Konzerthaus"
},
{
    "publication_date": "8.1.2021 4:18",
    "title": "Camerata Salzburg",
    "location": "Konzerthaus",
    "date_start": "2021-02-07"
}
1
  • 1
    It is possible. Commented Jan 8, 2021 at 3:40

2 Answers 2

2

You can use the map function on the array and create a new array. If your original data is also an array, you can map it first then map the inner array. This will create an array of arrays, but you can fall flat on it to merge them into a single array.

const data = [{
  "publication_date": "8.1.2021 4:18",
  "title": "Camerata Salzburg",
  "showtime": [
    {
      "date_start": "2021-02-05",
      "location": "Konzerthaus"
    },
    {
      "location": "Konzerthaus",
      "date_start": "2021-02-07"
    }
  ],
}]

const newData = data.map(title => title.showtime.map(e => ({
  publication_date: title.publication_date,
  title: title.title,
  date_start: e.date_start,
  location: e.location
}))).flat();

console.log(newData)

Sign up to request clarification or add additional context in comments.

Comments

0

Here is an iterative solution using object-scan

It's a bit easier to reason about in my opinion, but does require a dependency. So if you're new to javascript, I'd recommend you stick with @Todds solution for now. But maybe something to revisit once you gain a bit more experience and do more data processing.

// const objectScan = require('object-scan');

const myData = [{ publication_date: '8.1.2021 4:18', title: 'Camerata Salzburg', showtime: [ { date_start: '2021-02-05', location: 'Konzerthaus' }, { location: 'Konzerthaus', date_start: '2021-02-07' } ] }];

const flatten = (data) => objectScan(['[*].showtime[*]'], {
  reverse: false,
  filterFn: ({ value, gparent, context }) => {
    const { publication_date, title } = gparent;
    context.push({ publication_date, title, ...value });
  }
})(data, []);

console.log(flatten(myData));
/* => [
 { publication_date: '8.1.2021 4:18', title: 'Camerata Salzburg', date_start: '2021-02-05', location: 'Konzerthaus' },
 { publication_date: '8.1.2021 4:18', title: 'Camerata Salzburg', location: 'Konzerthaus', date_start: '2021-02-07' }
] */
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/[email protected]"></script>

Disclaimer: I'm the author of object-scan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.