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?
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
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()
Related
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
This is my JSON object and I need to check for a particular id and get the date
[
{
"id": "9b3b835b",
"date": "Tue Mar 27 10:23:18 UTC 2018"
},
{
"id": "57eab193",
"date": "Thu Mar 29 14:45:23 UTC 2018"
},
{
"id": "440f0cd9",
"date": "Thu Mar 29 15:12:00 UTC 2018"
},
]
yourVariable = [{
"id": "9b3b835b",
"date": "Tue Mar 27 10:23:18 UTC 2018"
},
{
"id": "57eab193",
"date": "Thu Mar 29 14:45:23 UTC 2018"
},
{
"id": "440f0cd9",
"date": "Thu Mar 29 15:12:00 UTC 2018"
},
];
for (keys in yourVariable) {
// supposing id your are looking for is 57eab193
if (yourVariable[keys].id == '57eab193') {
console.log(yourVariable[keys].date)
}
}
You can use the function find to get a specific according to a condition.
This alternative uses the logical operator || just to avoid access error over an undefined value.
var array = [{
"id": "9b3b835b",
"date": "Tue Mar 27 10:23:18 UTC 2018"
},
{
"id": "57eab193",
"date": "Thu Mar 29 14:45:23 UTC 2018"
},
{
"id": "440f0cd9",
"date": "Thu Mar 29 15:12:00 UTC 2018"
},
];
var date = (array.find(o => o.id === "440f0cd9") || {}).date;
console.log(date);
date = (array.find(o => o.id === "ele") || {}).date;
console.log(date);
Let's say that i have a JSON file like this
[
{
"programId": "bla-bla-1",
"programTitle": "bla bla 1",
"programImg": "bla1.jpg",
"startsAt": "Apr 05 2017 11:00:00 GMT+0200 (EET)",
"endsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)"
},
{
"programId": "bla-bla-2",
"programTitle": "bla bla 2",
"programImg": "bla2.jpg",
"startsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)",
"endsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)"
},
{
"programId": "bla-bla-3",
"programTitle": "bla bla 3",
"programImg": "bla3.jpg",
"startsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)",
"endsAt": "Apr 05 2017 16:00:00 GMT+0200 (EET)"
}
]
And current time is (Apr 05 2017 12:30:00 GMT+0200 (EET)) how can i get the current program from those time ranges using Javascript also i can use library like momentJS
You can do something like this:
var obj = [ { "programId": "bla-bla-1", "programTitle": "bla bla 1", "programImg": "bla1.jpg", "startsAt": "Apr 05 2017 11:00:00 GMT+0200 (EET)", "endsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)" }, { "programId": "bla-bla-2", "programTitle": "bla bla 2", "programImg": "bla2.jpg", "startsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)", "endsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)" }, {"programId": "bla-bla-3", "programTitle": "bla bla 3", "programImg": "bla3.jpg", "startsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)", "endsAt": "Apr 05 2017 16:00:00 GMT+0200 (EET)"}];
var now = new Date("Apr 05 2017 12:30:00 GMT+0200 (EET)").getTime();//new Date().getTime();
var prog = obj.find(o => {
return (now >= new Date(o.startsAt).getTime() && now <= new Date(o.endsAt).getTime());
});
console.log(prog);
Note that the solution given by Tisus may not work on every browser since date parsing of non-standard string is system-dependent.
I suggest to parse your input using moment(String, String) instead of new Date(String). Then you can use moment isBetween to check if the current day is in the given range.
Here a live sample:
var input = [
{
"programId": "bla-bla-1",
"programTitle": "bla bla 1",
"programImg": "bla1.jpg",
"startsAt": "Apr 05 2017 11:00:00 GMT+0200 (EET)",
"endsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)"
},
{
"programId": "bla-bla-2",
"programTitle": "bla bla 2",
"programImg": "bla2.jpg",
"startsAt": "Apr 05 2017 12:00:00 GMT+0200 (EET)",
"endsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)"
},
{
"programId": "bla-bla-3",
"programTitle": "bla bla 3",
"programImg": "bla3.jpg",
"startsAt": "Apr 05 2017 14:00:00 GMT+0200 (EET)",
"endsAt": "Apr 05 2017 16:00:00 GMT+0200 (EET)"
}
]
var prog = input.find(item => {
//var now = moment();
// mocking current time in order to get always the same result
var now = moment("Apr 05 2017 12:30:00 GMT+0200 (EET)", 'MMM DD YYYY HH:mm:ss Z')
var start = moment(item.startsAt, 'MMM DD YYYY HH:mm:ss Z');
var end = moment(item.endsAt, 'MMM DD YYYY HH:mm:ss Z');
return now.isBetween(start, end);
});
console.log(prog);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
As side note: I agree with Rajesh, providing code attempt would help getting better answers and gives you the chance to learn better.
I need to get rid of double quotes in my JSON string, this is how I am forming an array and then converting it to JSON
$dinnerDetails = array();
foreach ($dinners as $dinner) {
$dinnerDetails[] = array(
"title" => $dinner->getName(),
"data" => $dinner->getDate()
);
}
$dinnerDetails = json_encode($dinnerDetails);
Inside my view when i dump $dinnerDetails I see the following
string '[
{
"title": "Formal Dinner",
"data": "Tue Apr 05 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Formal Dinner",
"data": "Tue Apr 05 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Black Tie",
"data": "Wed Apr 13 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Formal Dinner",
"data": "Fri Apr 08 2016 05:00:00 GMT+0500 (PKT)"
}
]
and when I assign this to a JS variable this is how see it in the source
var dinners = [
{
"title": "Formal Dinner",
"data": "Tue Apr 05 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Formal Dinner",
"data": "Tue Apr 05 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Black Tie",
"data": "Wed Apr 13 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Formal Dinner",
"data": "Fri Apr 08 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Formal Dinner",
"data": "Sat Apr 16 2016 05:00:00 GMT+0500 (PKT)"
},
{
"title": "Formal Dinner",
"data": "Mon Mar 28 2016 05:00:00 GMT+0500 (PKT)"
}
];
I am using Symfony framework and this is how I am passing $dinnerDetails to view
return $this->render('AppBundle:admin/college:edit.html.twig', array(
'dinners' => $dinnerDetails
));
and then inside twig I am assigning to js variable like this
var dinner = jQuery.parseJSON(dinners);
In JS i even tried doing JSON.parse(dinners.replace(""",'"')); but this does not help.
The output that I am looking for is something like this
[
{
title: "All Day Event",
date: "Fri Apr 08 2016 05:00:00 GMT+0500",
},
{
title: "Long Event",
date: "Fri Apr 08 2016 05:00:00 GMT+0500",
},
]
I will really appreciate a help here.
try the raw-filter in your template. Something like this:
{{ dinners|raw }}
2nd. you have serialized a PHP-Object into a JSON-string.
If you paste this string inside a script-block this string will be interpreted as JS.
It is JS-code now, not a string anymore. You don't have to parse it; actually you cannot parse it.
<script> var dinner = {{ dinners|raw }}; </script>
Inside your controller or php file use below code.
<?php
$arrDinnerDetails = array();
foreach ($dinners as $dinner)
{
$arrDinnerDetails[] = array(
"title" => $dinner->getName(),
"data" => $dinner->getDate()
);
}
return $this->render('AppBundle:admin/college:edit.html.twig', array(
'arrDinnerDetails' => $arrDinnerDetails
));
?>
Inside your view file, use below code.
<script>
var strJsonDinner = JSON.stringify('<?php echo json_encode($arrDinnerDetails); ?>');
</script>
I have a json object as follows.
[{
"id": "1",
"username": "vishnu",
"FromDate": "Thu Apr 21 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id": "2",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui of creatwhimsy home page",
"time": "2"
}, {
"id": "3",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
}, {
"id": "4",
"username": "vishnu",
"FromDate": "Sat Jul 30 2016 12:03:20 GMT+0530 (India Standard Time",
"selectedProject": "timetracker",
"task": "working on ui of creatwhimsy home page and admin side home page",
"time": "1"
}, {
"id": "5",
"username": "vishnu",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}]
Let the object name be Data. I want to make a new object fro Data, but it should only contain "selectedProject" and "time".
How to do that in angularjs?
You can iterate and create a new object with desire data. For this you can use javascript push() function.
what you need to do is
$scope.log = [];
angular.forEach($scope.names, function(value, key){
$scope.log.push({"selectedProject":value.selectedProject,"time":value.time});
});
Here is JS fiddle: http://jsfiddle.net/kyaqzgo6/4/
A sample code for you:
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.names = [{
"id": "1",
"username": "vishnu",
"FromDate": "Thu Apr 21 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}, {
"id": "2",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "working on ui of creatwhimsy home page",
"time": "2"
}, {
"id": "3",
"username": "vishnu",
"FromDate": "Wed Mar 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "bigiron",
"task": "working on api",
"time": "5"
}, {
"id": "4",
"username": "vishnu",
"FromDate": "Sat Jul 30 2016 12:03:20 GMT+0530 (India Standard Time",
"selectedProject": "timetracker",
"task": "working on ui of creatwhimsy home page and admin side home page",
"time": "1"
}, {
"id": "5",
"username": "vishnu",
"FromDate": "Wed Jan 02 2016 10:56:45 GMT+0530 (India Standard Time)",
"selectedProject": "createwhimsy",
"task": "fixing bugs",
"time": "1"
}];
$scope.log = [];
angular.forEach($scope.names, function(value, key){
$scope.log.push({"selectedProject":value.selectedProject,"time":value.time});
});
console.log($scope.log);
});
</script>
Try something like this:
function SomeFunction() {
function getdata(callback, url) {
var obj= {};
$http.get(url).success(function(response) {
if (response.jsonname[0].length > 0) {
for (var i = 0; i < response.jsonname[0].length; i++) {
obj= {
selectedProject:response.jsonname[0][i].selectedProject,
time:response.jsonname[0][i].time
}
}
} else {
}
}).error(function() {
//something
}).then(function() {
callback(obj);
});;
}
return {
getdata: getdata
}
}
I would like to filter JavaScript objects by their property value. Here is a scenario:
The array below is a small example of what I'm working with
var array = [{
"Title": "July 13 - July 19 2014",
"displayAd_imp": "15,242,505",
"Date": "2014-07-17T00:00:00.000Z",
"WeekNo": 29
}, {
"Title": "July 20 - July 26 2014",
"displayAd_imp": "15,942,705",
"Date": "2014-07-24T00:00:00.000Z",
"WeekNo": 30
}, {
"Title": "July 27 - Aug 2 2014",
"displayAd_imp": "15,683,545",
"Date": "2014-07-31T00:00:00.000Z",
"WeekNo": 31
}, {
"Title": "Aug 3 - Aug 9 2014",
"displayAd_imp": "15,042,005",
"Date": "2014-08-07T00:00:00.000Z",
"WeekNo": 32
}, {
"Title": "Aug 10 - Aug 17 2014",
"displayAd_imp": "15,442,605",
"Date": "2014-08-14T00:00:00.000Z",
"WeekNo": 33
}]
In my app there are two dropdown fields that would let users pick a range of weeks. I'm calculating the week number by the "Date" and inserting the "WeekNo" as a property. I would like to use WeekNo to get a chunk of data based on the start-end values.
Example would be if start date is week29 and end is week32 the method would return the relevant data.
I have a Jsfiddle going if someone would like to update it.
Thanks a ton in advance!
You can use the Array.filter function. You call it directly on your array then provide a callback. If that callback returns true, the element is included in a new array that's returned from filter.
For example:
var newArr = array.filter(function(item) {
return (item.WeekNo >= 29 && item.WeekNo < 32);
});
Here's an updated JSfiddle with this included.
var array = [{
"Title": "July 13 - July 19 2014",
"displayAd_imp": "15,242,505",
"Date": "2014-07-17T00:00:00.000Z",
"WeekNo": 29
}, {
"Title": "July 20 - July 26 2014",
"displayAd_imp": "15,942,705",
"Date": "2014-07-24T00:00:00.000Z",
"WeekNo": 30
}, {
"Title": "July 27 - Aug 2 2014",
"displayAd_imp": "15,683,545",
"Date": "2014-07-31T00:00:00.000Z",
"WeekNo": 31
}, {
"Title": "Aug 3 - Aug 9 2014",
"displayAd_imp": "15,042,005",
"Date": "2014-08-07T00:00:00.000Z",
"WeekNo": 32
}, {
"Title": "Aug 10 - Aug 17 2014",
"displayAd_imp": "15,442,605",
"Date": "2014-08-14T00:00:00.000Z",
"WeekNo": 33
}]
var predicate = function(start, end) {
return function(array) {
return _.inRange(array.WeekNo, start, end + 1);
}
}
var result = _.filter(array, predicate(29, 32));
console.log('inRange', result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>