0

I'm trying to solve problem of merging two arrays of objects, so I wrote a pretty simple function

function mergeShowtimes(movies) {
    var existing_halls = [];

    return movies.reduce(function(acc, movie) {
        var movies_with_hallid,
            movies_seats;

        if (existing_halls.indexOf(movie.hallId) === -1) {
            movies_with_hallid = _.filter(movies, function(filter_movie) {
                return movie.hallId === filter_movie.hallId;
            });

            movies_seats = _.map(movies_with_hallid, 'seats');
            movie.seats = _.union.apply(this, movies_seats);
            acc.push(movie);
            existing_halls.push(movie.hallId);
        }

        return acc;
    }, []);
}

problem is, whenever I check the value of movie and movie.seats, field 'seats' in movie is empty, but movie.seats is not, for some reason

If I'll remove acc.push(movie); everything will start working as planned

What am I doing wrong?

Thanks for any feedback!

Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
Nausik
  • 735
  • 8
  • 22

0 Answers0