How to get data by month from JSON

1.7k Views Asked by At

I have this JSON:

[{"id":66,"price":56,"start":"Fri, 20 May 2016 00:00:00 +0000","user_id":8},{"id":65,"price":55.5,"start":"Wed, 18 May 2016 00:00:00 +0000","user_id":8},{"id":64,"price":55.5,"start":"Fri, 13 May 2016 00:00:00 +0000","user_id":8},{"id":63,"price":55.5,"start":"Thu, 12 May 2016 00:00:00 +0000","user_id":8},{"id":62,"price":55,"start":"Fri, 22 Apr 2016 00:00:00 +0000","user_id":8},{"id":61,"price":55.5,"start":"Thu, 28 Apr 2016 00:00:00 +0000","user_id":8},{"id":60,"price":54.5,"start":"Thu, 21 Apr 2016 00:00:00 +0000","user_id":8},{"id":59,"price":55,"start":"Wed, 20 Apr 2016 00:00:00 +0000","user_id":8}]

I create html from this data with:

function draw(data) {

   $.each(data, function(idx, obj) {
    $('#bid').append('<div class="row"><div class="col-md-2"><h5>'+obj.price+'</h5></div></div>');
});
};

but now I need to show only data for current month... How I can get data from JSON etc. only for April ? How to filter it only for month I need?

2

There are 2 best solutions below

1
Hackerman On BEST ANSWER

You can use something like indexOf:

var data = [{"id":66,"price":56,"start":"Fri, 20 May 2016 00:00:00 +0000","user_id":8},{"id":65,"price":55.5,"start":"Wed, 18 May 2016 00:00:00 +0000","user_id":8},{"id":64,"price":55.5,"start":"Fri, 13 May 2016 00:00:00 +0000","user_id":8},{"id":63,"price":55.5,"start":"Thu, 12 May 2016 00:00:00 +0000","user_id":8},{"id":62,"price":55,"start":"Fri, 22 Apr 2016 00:00:00 +0000","user_id":8},{"id":61,"price":55.5,"start":"Thu, 28 Apr 2016 00:00:00 +0000","user_id":8},{"id":60,"price":54.5,"start":"Thu, 21 Apr 2016 00:00:00 +0000","user_id":8},{"id":59,"price":55,"start":"Wed, 20 Apr 2016 00:00:00 +0000","user_id":8}];
     $.each(data, function(idx, obj) {
        if(obj.start.toString().indexOf('Apr') > -1){
           $('#bid').append('<div class="row"><div class="col-md-2"><h5>'+obj.price+'</h5></div></div>');
        }
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bid"></div>

 $.each(data, function(idx, obj) {
    if(obj.start.toString().indexOf('Apr') > -1){
       $('#bid').append('<div class="row"><div class="col-md-2"><h5>'+obj.price+'</h5></div></div>');
    }
 });

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

0
Alex Char On

Alternative you can check json start with current month like:

var currentMonth = new Date();//current date
$.each(data, function(idx, obj) {
  var d = new Date(obj.start);//json date 
  //compare json month with current month and if is equal append price
  if (d.getMonth() === currentMonth.getMonth()) {
    $('#bid').append('<div class="row"><div class="col-md-2"><h5>' + obj.price + '</h5></div></div>');
  }

});

var data = [{
  "id": 66,
  "price": 56,
  "start": "Fri, 20 May 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 65,
  "price": 55.5,
  "start": "Wed, 18 May 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 64,
  "price": 55.5,
  "start": "Fri, 13 May 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 63,
  "price": 55.5,
  "start": "Thu, 12 May 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 62,
  "price": 55,
  "start": "Fri, 22 Apr 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 61,
  "price": 55.5,
  "start": "Thu, 28 Apr 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 60,
  "price": 54.5,
  "start": "Thu, 21 Apr 2016 00:00:00 +0000",
  "user_id": 8
}, {
  "id": 59,
  "price": 55,
  "start": "Wed, 20 Apr 2016 00:00:00 +0000",
  "user_id": 8
}];
var currentMonth = new Date();//current date
$.each(data, function(idx, obj) {
  var d = new Date(obj.start);//json date 
  //compare json month with current month and if is equal append price
  if (d.getMonth() === currentMonth.getMonth()) {
    $('#bid').append('<div class="row"><div class="col-md-2"><h5>' + obj.price + '</h5></div></div>');
  }

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bid"></div>

References

Date.prototype.getMonth()