Merge objects based on date in javascript array - javascript

I have an object
{
YHOO: [
{
date: Fri Apr 12 1996 00:00:00 GMT-0400 (EDT),
open: 25.25,
high: 43,
low: 24.5,
close: 33,
volume: 408720000,
adjClose: 1.38,
symbol: 'YHOO'
},
...
{
date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
open: 35.07,
high: 35.89,
low: 34.76,
close: 35.69,
volume: 21368600,
adjClose: 35.69,
symbol: 'YHOO'
}
],
GOOGL: [
{
date: Thu Aug 19 2004 00:00:00 GMT-0400 (EDT),
open: 100,
high: 104.06,
low: 95.96,
close: 100.34,
volume: 22351900,
adjClose: 100.34,
symbol: 'GOOGL'
},
...
{
date: Thu Nov 14 2013 00:00:00 GMT-0500 (EST),
open: 1033.92,
high: 1039.75,
low: 1030.35,
close: 1035.23,
volume: 1166700,
adjClose: 1035.23,
symbol: 'GOOGL'
}
],
...
}
How do I alter this object so it becomes an array with
[
{
date: Fri Apr 12 1996 00:00:00 GMT-0400 (EDT),
YHOO: 33,
GOOG: 100.34
},
...
]
I know it can become a problem that they potentially not have the same dates, so I guess I should just say that one of the companies have the right dates, and then merge the other companies to these dates and omit those dates that are not in the already selected company.
I guess I should do something like
var data = [];
obj.YHOO.forEach((quote) => {
data.push({
date: quote.date,
YHOO: quote.price,
GOOG: ?
});
});
and instead of the question mark, I can loop through all the objects in array.GOOG and check if the date matches quote.date.
The big problem is that this seems quite computational heavy and the names of the companies can vary, so I cannot guarantee that YHOO and GOOG are present.

Related

Pass date range picker value to filter

I am using daterangepicker to select the start and the end date.
This is my JsFiddle example
The date is working and I can select the start and the end date.
<input type="text" class="date" ng-model="selectDate" />
But how can I pass the selectDate model to the filters so that only those events will be selected where selectDate will match the eventStartDateTime
$scope.data=[{'eventStartDateTime': 'Tue, 02 April 2019, 12:30 PM','eventName': 'ANew Event','itemCreatedDateTime': '3/04/2019 5:17:10 AM',},{'eventStartDateTime': 'Tue, 02 April 2019, 02:43 PM','eventName': 'AFeatured Event 3','itemCreatedDateTime': '2/04/2019 1:54:10 AM',},{'eventStartDateTime': 'Tue, 02 April 2019, 12:30 PM','eventName': 'Event 9','itemCreatedDateTime': '2/04/2019 1:29:56 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 04:30 AM','eventName': 'Featured Event 2','itemCreatedDateTime': '28/03/2019 4:59:13 AM',},{'eventStartDateTime': 'Tue, 02 April 2019, 12:55 PM','eventName': 'Featured Event 4','itemCreatedDateTime': '28/03/2019 4:58:54 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 04:30 AM','eventName': 'Avent 5','itemCreatedDateTime': '28/03/2019 1:29:06 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 05:30 AM','eventName': 'Event 4','itemCreatedDateTime': '28/03/2019 1:29:00 AM',},{'eventStartDateTime': 'Fri, 29 March 2019, 04:00 AM','eventName': 'Event 3','itemCreatedDateTime': '28/03/2019 1:28:54 AM',},{'eventStartDateTime': 'Thu, 21 March 2019, 04:30 AM','eventName': 'Event 2','itemCreatedDateTime': '28/03/2019 1:28:41 AM',},{'eventStartDateTime': 'Thu, 28 March 2019, 04:00 AM','eventName': 'Event 1','itemCreatedDateTime': '28/03/2019 1:28:36 AM',}];
Any help or suggestion would be appreciated.
Thanks in advance
You can use the Angular.js directive for daterangepicker
Install it then add daterangepicker to your angular.module and initialize your variables:
var app = angular.module("myApp", ["daterangepicker"]);
app.controller("myCtrl", function($scope, $window) {
...
$scope.showFreeEvent = false;
$scope.selectDate = { date: { startDate: null, endDate: null } };
...
Then in your HTML add attribute date-range-picker to any input and bind it to model:
<input
date-range-picker
class="form-control date-picker"
type="text"
ng-model="selectDate.date"
/>
And to filter your events you can use moment().isBefore() and moment().isAfter():
if (!$scope.showFreeEvent) {
return true;
}
if (
$scope.selectDate.date.startDate.isAfter(el.eventStartDateTime) ||
$scope.selectDate.date.endDate.isBefore(el.eventStartDateTime)
) {
return false;
}
Demo: https://codesandbox.io/s/l29yqywx9m
add ng-onchange="filterfunction(dateModelInput)"
this will detect a change in your html,call your filter function. this will update your DOM.
make sure you include the filter into the html that needs the filtering.
<div>{{ctrl.data | filterResult }}</div>

Moment.js formatting incorrect

I'm using moment.js for my react-native project datetime formater. I want to remove the timezone from utc date. For my timezone is GMT+8000, so for utc offset is correct, when when I add format('YYYY/MM/DD 00:00'), result subtract one day.
console.log(moment.utc());
-> Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Jul 11 2018 11:08:57 GMT+0800 (Malaysia Time), …}
console.log(moment.utc().utcOffset(new Date().getTimezoneOffset()));
->Moment {_isAMomentObject: true, _isUTC: true, _pf: {…}, _locale: Locale, _d: Wed Jul 11 2018 03:08:57 GMT+0800 (Malaysia Time), …}
console.log(moment.utc().utcOffset(new Date().getTimezoneOffset()).format('YYYY/MM/DD 00:00'));
->"2018/07/10 00:00"
Any idea how ? Or maybe I doing it wrong ?

window.open is not working in IE to download an event

I'm trying to create an ICS file on click of buttons inside each of my divs.
This works fine in Chrome and Firefox. However, it fails to work on IE.
What's wrong with the code in 'window.open'?
Also, how do I change the name of th file based on the event ID?
Here's the code
angular.module('myApp', []).controller('myCtrl', function($scope){
$scope.card = [{
Name: "New Year Celebration",
Description: "",
Venue: "",
StartDate: "Fri Dec 29 2017 23:30:00 GMT+0530",
EndDate: "Sat Dec 30 2017 00:30:00 GMT+0530",
EventID: "1"
}, {
Name: "25th Anniversary Celebration",
Description: "25th Anniversary Celebration of organization",
Venue: "Auditorium",
StartDate: "Wed May 31 2017 17:30:00 GMT+0530",
EndDate: "Wed May 31 2017 20:30:00 GMT+0530",
EventID: "2"
}, {
Name: "Annual Day",
Description: "",
Venue: "",
StartDate: "Fri Oct 13 2017 14:30:00 GMT+0530",
EndDate: "Fri Oct 13 2017 17:30:00 GMT+0530",
EventID: "3"
}];
$scope.add = function(eventObj) {
$scope.eventID= this.eventObj.EventID;
$scope.startDate= this.eventObj.StartDate;
$scope.endDate= this.eventObj.EndDate;
$scope.venue= this.eventObj.Venue;
$scope.subject= this.eventObj.Name;
$scope.result= this.eventObj.Description;
//console.log(this);
$scope.icsMSG = "BEGIN:VCALENDAR\nVERSION:2.0\nBEGIN:VEVENT\nDTSTART:" + $scope.startDate +"\nDTEND:" + $scope.endDate +"\nLOCATION:" + $scope.venue + "\nSUMMARY:" + $scope.subject + "\nDESCRIPTION:"+ $scope.result +"\nEND:VEVENT\nEND:VCALENDAR";
window.open("data:text/calendar;charset=utf8," + escape($scope.icsMSG),"_self");
};
});
.event {
height: 150px;
width: 250px;
border: 1px solid lightgrey;
background-color: skyblue;
margin: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="eventObj in card" class="event">
Subject: <span>{{eventObj.Name}}</span>
<br /><br />
Venue:<span>{{eventObj.Venue}}</span>
<br /><br />
Date:<span>{{eventObj.StartDate | date:'fullDate'}}</span>
<br /><br />
<button ng-click="add(eventObj.EventID)">Add to Outlook</button>
</div>
</div>
Data URIs cannot be used for navigation, for scripting, or to populate frame or iframe elements in IE.
The solution is to use navigator.msSaveBlob to generate the file and prompt the user to save it. Refer this answer
You could also use something like downloadify instead of data URLs (would work for IE as well) as mentioned here

Unable to parse a JSON in jade

I am trying to parse a JSON object that comes in from mongodb but am unable to use it for some reason after parsing.
Here is the piece of javascript function that I am using
-function check(category) {
-var temp = JSON.stringify(category);
-return JSON.parse(temp);
-}
.row.wow.fadeInRight.animated(data-wow-offset='30', data-wow-duration='1.5s', data-wow-delay='0.15s')
.col-md-12
if(articles)
#client-feedbacks.owl-carousel.owl-theme
each article in articles
-var category = article.categories
.feedback-box
.client-image.hidden-xs
img(src=article._.image.fit(250,250), alt='article image')
// MESSAGE OF THE CLIENT
h6.article-title
a(href='/blog/post/' + article.slug, itemprop='url')!= article.title
br
.message!= article.content.brief
// CLIENT INFORMATION
.client
.quote.red-text
i.icon-fontawesome-webfont-294
.client-info
a.client-namea(href='/blog/post/' + article.slug, itemprop='url')!= prettyDate(article.publishedDate)
-var categories = check(category)
.client-company(itemtype="http://schema.org/BlogPosting") #{category[0].name}
Here is what the object looks like when i use json .stringify
{ updatedBy: 5498631c5cbfa2d03f4570e6,
updatedAt: Mon Feb 09 2015 00:30:48 GMT+0530 (IST),
createdBy: 5498631c5cbfa2d03f4570e6,
createdAt: Mon Feb 09 2015 00:30:48 GMT+0530 (IST),
key: 'mongoose',
name: 'Mongoose',
_id: 54d7b2601d88fc4f69d0e081,
__v: 0 },{ updatedBy: 5498631c5cbfa2d03f4570e6,
updatedAt: Mon Feb 09 2015 00:31:44 GMT+0530 (IST),
createdBy: 5498631c5cbfa2d03f4570e6,
createdAt: Mon Feb 09 2015 00:31:44 GMT+0530 (IST),
key: 'green-berrets',
name: 'Green Berrets',
_id: 54d7b298fe85a1c4698b2ec3,
__v: 0 }
I am confused on how to parse it as most of the things i tried did not work

Why does this return an invalid date with moment js

This
moment(1420066800000);
returns a moment object with an invalid date, why?
EDIT:
moment(1420066800000).isValid()
returns true! But: the moments' property
_d.__proto__
is an "Invalid Date" object. That's what confused my, since I thought this was causing wrong behaviour in my application.
What is wrong with Wed Dec 31 2014 23:00:00 GMT+0000 (UTC) ?
> d = moment(1420066800000);
{ _isAMomentObject: true,
_i: 1420066800000,
_f: undefined,
_l: undefined,
_strict: undefined,
_isUTC: false,
_pf:
{ empty: false,
unusedTokens: [],
unusedInput: [],
overflow: -2,
charsLeftOver: 0,
nullInput: false,
invalidMonth: null,
invalidFormat: false,
userInvalidated: false,
iso: false },
_d: Wed Dec 31 2014 23:00:00 GMT+0000 (UTC) }

Categories