rendering nested keys in a template - javascript

i have a grouped array that i want to render on the front end via PUG and it's proving a little tricky
here's the array
{
"Apr 14th 19": {
"5:00 PM": [
{
"name": "John",
"message": "Hey there"
},
{
"name": "Josh",
"message": "Hey"
}
]
},
"Apr 15th 19": {
"5:00 PM": [
{
"name": "Jake",
"message": "Hey you"
}
]
}
}
here is my pug code that im trying to use to render the kind of template i want (desired output below)
each day in Data
each hour in day
each entry in hour
h2= "The date is" + Object.keys(day)
h2= "The time is" + Object.keys(hour)
h2= "The message is" + entry.message
the kind of template output i want
The date is Apr 14th 19
The time is 5:00 pm
The message is : Hey
The message is : Hey you
(Both messages here because those are 2 nested under under the times)
none of this actually works, i can really use some help

You'll want to do something along these lines within your Pug template:
each dayObj, day in Data
h2= "The date is " + day
each hourObj, hour in dayObj
h2= "The time is " + hour
each entry in hourObj
h2= "The message is : " + entry.message
Here's a working Pen: https://codepen.io/chanceaclark/pen/JVpmMd

Related

Mongo DB field convert to 12 hrs format from 24 hrs format

I need to convert and update date fields in 12 hrs format in one Collection(demo) through Mongo Db script.
Sample data is given below;
[
{
"_id": "dgdjh13233",
"a": [
{
"b": [
{
"date": "03/26/2022 23.03 pm"
},
{
"date": "03/26/2022 04.03 pm"
}
]
}
]
},
{
"_id": "dgdjh132365",
"a": [
{
"b": [
{
"date": "03/26/2022 22.03 pm"
},
{
"date": "03/28/2022 08.03 pm"
}
]
}
]
}
]
I need to update date fields for ex "date":"03/26/2022 22.03 pm" to "03/26/2022 10.03 pm".
Is there any way to do this throgh scripts or can we find which all the data which are in 24 hrs format so that we can change it manually?
I am trying to fetch data throug scripts which are in 24 hours format but unable to do due to inadeqaute knowledge of mongodb.
db.getCollection("demo").find({},
{
"a.b.date":"$a.b.date",
"_id":NumberInt(0)
}
);
"03/26/2022 23.03 pm" is a string, so if you want to convert from 12 to 24 hour in the mongod server, you will first need to convert that to a datetime with $dateFromString and then convert it back to a string in the format you want with $dateToString

Filtering Array of Objects with Subcategories from an array of dates in Milliseconds, then ng-repeating depending upon date in calendar

So I have a JSON object of an array of objects that looks something like this:
[
{
"id": 0,
"name": "Sophia Mason",
"availability": [
{
"date": 1522216800000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
{
"date": 1522303200000,
"times": ["9:00 am", "2:30 pm", "5:00 pm", "6:00 pm"]
},
...
],
},
.........
]
I have another array of days rendered using moment.js that looks something like this:
[1522216800000, 1522303200000, 1522389600000]
I need to filter the first array of objects availability by the day in each index of the date array. Then render each photographer in each day correctly in a calendar.
I am using AngularJS (by request of the client) to make something that looks like this:
I already have the days rendering by using Moment, but I am having trouble figuring out how to show each photographer per day based on their availability. To look like the calendar above.
I may be overthinking this but I have been trying to figure this out for the last few hours. Here are some of the many 'similar' answers I have looked through:
Stack
Stack
Stack
Which has me only thinking of something that looks like this in my controller.js:
function setPhotographers() {
let photographer = photographers.filter((el) => {
el.availability.some((availability) => availability.date === today)
.map((el)=> {
console.log(el);
let newEl = Object.assign({}, el);
return newEl.availability.filter(availability => availability.date === today);
});
});
return photographer;
}
I am still am learning a lot but this has me pretty stuck. Any help would be great! Please if you have an answer to describe why your solution works to help me better understand what you did to accomplish this.
Thanks!
You're on the right track with the .filter(), I'm not sure why you need the .map(). The following should be sufficient to get you a filtered array of photographers with availability matching the today variable.
let availablePhotographers = photographers.filter((el) => el.availability.some((availability) => availability.date === today))

Parse json using javascript with multiple object set

I have a below output from the server.And I want to get the "result" from the JSON input.
My JSON Input is
{
"header":{
"type":"esummary",
"version":"0.3"
},
"result":{
"28885854":{
"uid":"28885854",
"pubdate":"2017 Sep 8",
"epubdate":"2017 Sep 8",
"source":"J Org Chem",
}
}
}
And I prefer only javascript.Anyone help me to do this.
EDIT :
I tried the code to get JSON from server
var json = JSON.stringify(temp1);
console.log(json)
My original output in the console was like this.
"
{"header":{"type":"esummary","version":"0.3"},"result":{"28885854":{"uid":"28885854","pubdate":"2017 Sep 8","epubdate":"2017 Sep 8","source":"J Org Chem","authors":[{"name":"Farmer LA","authtype":"Author","clusterid":""},{"name":"Haidasz EA","authtype":"Author","clusterid":""},{"name":"Griesser M","authtype":"Author","clusterid":""},{"name":"Pratt DA","authtype":"Author","clusterid":""}],"lastauthor":"Pratt DA","title":"Phenoxazine: A Privileged Scaffold for Radical-Trapping Antioxidants.","sorttitle":"phenoxazine a privileged scaffold for radical trapping antioxidants","volume":"","issue":"","pages":"","lang":["eng"],"nlmuniqueid":"2985193R","issn":"0022-3263","essn":"1520-6904","pubtype":["Journal Article"],"recordstatus":"PubMed - as supplied by publisher","pubstatus":"10","articleids":[{"idtype":"pubmed","idtypen":1,"value":"28885854"},{"idtype":"doi","idtypen":3,"value":"10.1021/acs.joc.7b02025"},{"idtype":"rid","idtypen":8,"value":"28885854"},{"idtype":"eid","idtypen":8,"value":"28885854"}],"history":[{"pubstatus":"entrez","date":"2017/09/09 06:00"},{"pubstatus":"pubmed","date":"2017/09/09 06:00"},{"pubstatus":"medline","date":"2017/09/09 06:00"}],"references":[],"attributes":["Has Abstract"],"pmcrefcount":"","fulljournalname":"The Journal of organic chemistry","elocationid":"doi: 10.1021/acs.joc.7b02025","doctype":"citation","srccontriblist":[],"booktitle":"","medium":"","edition":"","publisherlocation":"","publishername":"","srcdate":"","reportnumber":"","availablefromurl":"","locationlabel":"","doccontriblist":[],"docdate":"","bookname":"","chapter":"","sortpubdate":"2017/09/08 00:00","sortfirstauthor":"Farmer LA","vernaculartitle":""},"uids":["28885854"]}}
"
I think all you're looking for is .result. Perhaps you also want JSON.parse(...) if you have just the JSON string so far.
var obj = {
"header": {
"type": "esummary",
"version": "0.3"
},
"result": {
"28885854": {
"uid": "28885854",
"pubdate": "2017 Sep 8",
"epubdate": "2017 Sep 8",
"source": "J Org Chem",
}
}
};
console.log(obj.result);
// To address edits above and comments below:
console.log(obj.result["28885854"].source);

how to sort time values based on From time in using filters in angular

This is my sample data:
{
"day": "Sunday",
"count": [{
"desc": "sunday one event"
}, {
"j": [{
"start": "11:00 pm"
}, {
"end": "11:51 pm"
}]
}, {
"desc": "sunday second event"
}, {
"j": [{
"start": "12:00 am"
}, {
"end": "12:06 am"
}]
}]
}
And then output will be:
sunday one event
11:00 PM, 11:51 PM,
sunday second event
12:00 AM, 12:06 AM,
But I want to show data as:
sunday second event
12:00 AM, 12:06 AM,
sunday one event
11:00 PM, 11:51 PM,
How to sort-out data based on timing (start-end)
I think you should have a id on each row, then this line of code will sort you reverse order.
"<tr ng-repeat="obj in object | orderBy:propertyName:reverse">"
the best way is to store a Date object and compare the getTime(), of these object to sort by date.

Invalid date is appearing for everything when I use Configuration Generator code

Using the Configuration Generator from http://www.daterangepicker.com/#examples, I selected "ranges (with example predefined ranges)" and got the following JS:
$('#demo').daterangepicker({
"ranges": {
"Today": [
"2016-10-17T04:27:01.501Z",
"2016-10-17T04:27:01.501Z"
],
"Yesterday": [
"2016-10-16T04:27:01.501Z",
"2016-10-16T04:27:01.501Z"
],
"Last 7 Days": [
"2016-10-11T04:27:01.501Z",
"2016-10-17T04:27:01.501Z"
],
"Last 30 Days": [
"2016-09-18T04:27:01.501Z",
"2016-10-17T04:27:01.501Z"
],
"This Month": [
"2016-10-01T05:00:00.000Z",
"2016-11-01T04:59:59.999Z"
],
"Last Month": [
"2016-09-01T05:00:00.000Z",
"2016-10-01T04:59:59.999Z"
]
},
"startDate": "10/10/2016",
"endDate": "10/16/2016"
}, function(start, end, label) {
console.log("New date range selected: ' + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD') + ' (predefined range: ' + label + ')");
});
Unfortunately, when I use that, every date is invalid. Here's a JS fiddle demonstrating what I mean:
https://jsfiddle.net/LgetLa6c/
If you hover over anything it'll say "Invalid date". The only thing that doesn't say "Invalid date" is "Custom Range".
I figured it out. Instead of having dates formatted like "2016-10-17T04:27:01.501Z" the dates need to be formatted like "10/17/2016".
The fact that the examples in the documentation don't work is pretty dumb meh.

Categories

Resources