javascript push removing previous item from array

Posted 1 month ago by Gabotronix

Hi, I have a collection of posts wich I want to group by month of creation (created_at), to do this I'm looping through each post, parsing the date using moments plugin and pushing to the correct array, however there's a problem, I end up with an array of 12 items (months) with only one post per month, I think this is because when I push into array the previous item gets replaced.

My logic:

import axios from 'axios';
import _ from 'lodash';
const state = {
    posts: [],
    months : { Enero:[], Febrero:[], Marzo:[], Abril:[], Mayo:[], Junio:[], Julio:[], Agosto:[], Septiembre:[], Octubre:[], Noviembre:[], Diciembre:[] },
    postcategories: [],

const getters = {

    postsByMonth: (state) => {
        return _.forEach(state.posts, function(post) {
            let month = _.upperFirst(Vue.moment(post.created_at).format('MMMM'));


I end up with this, as you can see they aren't even properly grouped:

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.