2

See live demo

The following code takes data from an XML file and outputs each item in div="item"

I've converted the date so that instead of the full date you get it in YYYYMMDD (e.g 20131216)

var date     = new Date( $item.find('pubDate').text() );
                var yyyymmdd = date.getFullYear() +''+ (date.getMonth()+1) +''+ date.getDate();
                array += '<p>' + yyyymmdd + '</p>';

1. But now I need to sort this data and store it in a sub-array, so that only the last 30 days are stored in the sub-array.

Full script:

jQuery(function(){
        $.ajax({
          url: 'http://www.sagittarius-digital.com/news.rss',
          dataType: 'xml'
        }).done(function(xml){
          var items = $(xml).find('item').map(function(){
                var $item = $(this);
                var array = '<div class="item">';
                array += '<a href="' + $item.find('link').text() + '">';
                array += '<h2>' + $item.find('title').text() + '</h2>';
                array += '<p>' + $item.find('description').text() + '</p>';
                var date     = new Date( $item.find('pubDate').text() );
                var yyyymmdd = date.getFullYear() +''+ (date.getMonth()+1) +''+ date.getDate();
                array += '<p>' + yyyymmdd + '</p>';
                array += '<p>Category: ' + $item.find('category').text() + '</p>';
                array += '</a>';
                array += '</div>';
                return array;
          }).get();
          $('div.item').append(items.join(' '));
        }).fail(function(){
          console.log('error', arguments)
        })
      })

2. And then I need to randomise the output of that sub-array so that the last X items (from last 30 days) output in a random order. This will ensure the data is "recent", but doesn't show in perfect chrono order.

Does anyone have any ideas?

Francesca
  • 26,842
  • 28
  • 90
  • 153

1 Answers1

0

Seems easiest to just exclude everything older that 30 days in the loop

jQuery(function () {
    $.ajax({
        url: 'http://www.sagittarius-digital.com/news.rss',
        dataType: 'xml'
    }).done(function (xml) {
        var items = [];
        $(xml).find('item').each(function () {
            var $item    = $(this);
            var date     = new Date($item.find('pubDate').text());
            var date_30  = new Date().getTime() - (1000*60*60*24*30);
            var yyyymmdd = date.getFullYear() + '' + (date.getMonth() + 1) + '' + date.getDate();

            if ( date_30 < date.getTime() ) { // newer than 30 days
                var array = '<div class="item">';
                array += '<a href="' + $item.find('link').text() + '">';
                array += '<h2>' + $item.find('title').text() + '</h2>';
                array += '<p>' + $item.find('description').text() + '</p>';

                array += '<p>' + yyyymmdd + '</p>';
                array += '<p>Category: ' + $item.find('category').text() + '</p>';
                array += '</a>';
                array += '</div>';
                items.push(array);
            }
        });
        $('div.item').append(items.join(' '));
    }).fail(function () {
        console.log('error', arguments)
    });
});

If you still need to shuffle the array, here's a solution for that

Community
  • 1
  • 1
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Does seem like a more sensible solution but alas it doesn't work. See http://www.sagittarius-digital.com/sag-aggregator/sag-aggregator.html – Francesca Jan 03 '14 at 10:46
  • Weird, I waited on it for about 20 minutes and it did nothing, now seems to be working. Thanks! Any ideas why it is outputting a second "item" div, rather than container > items, I am getting container > item > items – Francesca Jan 03 '14 at 11:35