0

I am trying to create an archive system from google blogger api. I have the data as json of all posts from the blogger. Here is my code that I have tried.

var archive= {};
if("entry" in TotalFeed.feed) 
{
    var PostEntries=TotalFeed.feed.entry.length;
    for(var PostNum=0; PostNum<PostEntries ; PostNum++) 
    {
        var ThisPost = TotalFeed.feed.entry[PostNum];
        var key=ThisPost.published.$t.substring(0,4);
        var MonthNames=["January","February","March","April","May","June","July","August","September","October","November","December"];

        var m=ThisPost.published.$t.substring(5,7);
        var NameOfMonth = MonthNames[parseInt(m)-1];

         archive[key] = {
            [NameOfMonth]: 1
         }
    }
}
console.log(archive);

The result of this code is

2011:{May: "1"},
2012:{January: "1"},
2013:{January: "1"}

There are many months in 2011 but you can see it only overwrite the last one. So I only get last month of the year from the loop. How can I get all the months listed?

Any idea or help is highly appreciated. Thank you.

Edit: Key is not same, Here is a picture of the log enter image description here

4
  • It looks like the problem is that the key in the loop is always being the same, so when you do archive[key] = {[NameOfMonth]: 1 } it overwrites the previous data. Commented Jan 25, 2018 at 12:59
  • It's true that it's same for some cases but there are different months for sure. Commented Jan 25, 2018 at 13:01
  • can you create a codepen with the data? It'll be better to check the problem Commented Jan 25, 2018 at 13:03
  • Ok creating and thank you Commented Jan 25, 2018 at 13:07

1 Answer 1

1

You need a deeper object and you are also not checking if archive[key] already exists so you overwrite it if it does.

Try something like

// existing year object or create it
archive[key] = archive[key] || {};
//existing month array or create it
archive[key][NameOfMonth] = archive[key][NameOfMonth] || [];
// push item to appropriate month array
archive[key][NameOfMonth].push(ThisPost)

Or just to count year/month entries:

// existing year object or create it
archive[key] = archive[key] || {};
//existing month property or create it then increment count
archive[key][NameOfMonth] = (archive[key][NameOfMonth] || 0) +1;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, one problem that I am trying to solve. My data is in descender order of the month like 2017 2016 but it always starts ascending order from 2011 2012.... Any idea? Thank you so much
Could map to an array. var res = Object.keys(archive).sort((a-b)=>b-a).map(key=>({year:key, items: archive[key]})...then do same mapping concept on months(items) to get them sorted

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.