I have a situation here. I am having a object like this.
var x ={
"startTime":"10:05Am",
"endTime":"12:05Pm"
}
So my requirement is i want a function which will return a variable with respective time
function getTime(x){
//do something
return timewitHrs;
}
timewitHrs should can be something like this
[{"10am-11pm":55,"11pm-12pm":60,"12pm-1pm":5}]
Is there any way to sort this out
Thanks.
You may use following code
var x ={
"startTime":"10:05am",
"endTime":"11:05am"
}
var start=moment(x.startTime,"hh:mma");
var end=moment(x.endTime,"hh:mma");
var output={};
output.duration=end.diff(start, 'm');//diference in minutes
output.range=start.startOf('h').format("hha")+"-"+end.endOf('h').add(1,'s').format("hha");
output:{ duration: 60, range: '10am-12pm' }
Here I am calculating range based on start time & end time.
If you want something different let me know.
Related
here I have an example of a case in the project I'm working on.
I have data like this => https://run.mocky.io/v3/1e7e5b56-1ae7-4212-92a1-1b233707f495
then, I want to console it with output like this =>
("On #timestamp there were 12x api hits based on data.message").
so the question is, how do you calculate the #timestamp to be 1 day and calculate the total data.message in 1 day based on the #timestamp?
Thank you very much in advance, I really appreciate all your answers.
Something like that would work:
const count = {};
yourData.forEach(item => {
const currentDateate = getDate(item['#timestamp']);
if (!count[currentDateate]) {
count[currentDateate] = 0;
}
count[currentDateate]++;
});
console.log(count)
Where the getDate function would be a function that converts timestamp to date. And yourData would be the inner array in your "data", something like like data[1]
I can not find any solutions. Please help me.
My expectation is:
["IAfpK","WNVdi","JukI","hjgut","gjhg"]
First you need to send the data from API in below JSON format
var dt=[{ 'key':'IAfpK', 'age':58 },
{ 'key':'WNVdi', 'age':30 }]
Then Apply Filter and return key where age is 30
dt.filter(x=>x.age==30).map(x=> {return x.key})
Or if you don't want to change API which I suggest to change try below.
var data={"data":"key=IAfpK, age=58, key=WNVdi, age=64, key=jp9zt, age=47, key=0Sr4C, age=68}
var model = data.data.split(',')
var result = d.data.split(',').map((x,i)=>{if(x.trim()=='age=30'){return [i-1];}}).filter(x=>x!=undefined).map(x=>{return model[x].split('=')[1]})
Let's say I have this variable:
var myvar = {
welcome: "Welcome!",
thx: "Thank you!",
...
}
Then, I have a function, which gets sent a string and return the value from that variable:
function myFunction(key){
return myvar[key]
}
So this happens:
console.log(myFunction('welcome')) ///prints Welcome!
That's all great, and works beautifully, but what if then I wanna add something like months to the original variable, and I want something like this
var myvar={
.
.
months: [
["jan", "January"],
..
}
So if I wanna call for example, January, I'd do
myvar[months][0][1] //Select the months part, 0 means it's january, 1 means its fully written not just "jan"
I could, for example, in my key do something like months-0-1 and split it to get all 3 keys; but how could I adapt my original function to work for both the original content (welcome, thx) and for the months?
Second part to this question, should I even do it or would it be a not very optimal solution, should I just go with the whole adding each entry into the variable like before method?
Note: I still want the answer to my first question, if nothing else because I want to know how it could be done, even if I don't end up doing it.
A bit more information of the use case, I have a data-translate tag on some objects in my html, and some that are generated dynamically, these variables are for language translation, and they all call that function either when they are made or when the language changes. (data-translate="welcome") for example
You can update your function code to be like below and pass keys as - separated values as you have mentioned. Refer this article Array.prototype.reduce() if you are not familiar with it.
return key.split('-').reduce((a, i) => a[i], myvar);
Try it below.
var myvar = {
welcome: "Welcome!",
thx: "Thank you!",
months: [
["jan", "January"]
]
};
function myFunction(key) {
return key.split('-').reduce((a, i) => a[i], myvar);
}
console.log(myFunction('welcome')); // prints Welcome!
console.log(myFunction('months-0-1')); // prints January!
We can do something like this
var myvar = {
welcome: "Welcome!",
thx: "Thank you!",
months: [
["jan", "January"]]
};
function myFunction(key){
let keys = key.split("-");
let len = keys.length;
let result = myvar, i=0;
while(i < len)
result = result[keys[i++]];
return result;
};
console.log(myFunction("welcome"), myFunction("months-0-0"))
I have to retrieve data from an API.
Certain data has to be retrieved in a certain order.
To be exact, data needs to be retrieved in this order:
7,40,8,9,10,45,11,39,5,12,13,15,6,18,0,46,22,23,3,41,1,24,42,25,26,4,27,2
So when loop is doing 0, then it needs to retrieve data number 7, when loop is doing 1, then data number 40 and if loop is doing 2 then data number 8 etc.
listWithDataFromAPI I do this:
metricsSheet.appendRow([listWithDataFromAPI.symbols]);
And get this response:
[Ljava.lang.Object;#1e1aaea2
When I insert a specific number I do this:
metricsSheet.appendRow([listWithDataFromAPI.symbols].symbols[8]]);
And get such response: {name=DataPeter, longVolume=6640.87, longPositions=23678}
Thus if loop is 0 then extract 7, loop is 2 extract 40 etc. as I mentioned.
This is what I'm trying in concept:
var listNumberValue = ["5","30","7"];
var symbols = listWithDataFromAPI.symbols;
for (var i in listNumberValue) {
var symbol = symbols[listNumberValue];
metricsSheet.appendRow([symbol.name]);
}
Hope this makes sense.
Not sure how to do this..?
The big problem for us is that we don't know what listWithDataFromAPI is and you have not explained it. But I think you're trying to say that you want to iterate over something that you can get from it so I took a stab at what I think you're trying to do.
The first function will take your 0 through 46 indexes and reorder as 7,40,8,9,10,45,11,39,5,12,13,15,6,18,0,46,22,23,3,41,1,24,42,25,26,4,27,2
As shown in this table.
function getIdxObj() {
var idxA=[7,40,8,9,10,45,11,39,5,12,13,15,6,18,0,46,22,23,3,41,1,24,42,25,26,4,27,2];
var idxObj={};
for(var i=0;i<idxA.length;i++) {
idxObj[i]=idxA[i];
}
return idxObj;
}
The second function loops over symbols which apparently come from listWithDataFromAPI.symbols which has not been explained and so we know nothing about it.
function theUnknownFunction() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('metrics');//not sure about this either
var idxObj=getIdxObj();
var symbols = listWithDataFromAPI.symbols; //I dont have a clue as to what listWithDataFromAPI is
for (var i=0;i<symbols.length;i++) {
var symbol = symbols[idxObj[i]];
sh.appendRow([symbol.name]);
}
}
Hi please see my plunkr below
https://plnkr.co/edit/8cJZsb?p=preview
I have $scope.data which looks like this
$scope.data = [
{
"projectedStart":"2017-01-20T00:00:00"
},
{
"projectedStart":"2017-02-09T00:00:00"
}
];
and $scope.possibleDates that look like this
$scope.possibleDates = [{
"projectedStartDate":"2017-01-25T00:00:00",
"dateName":"January - Week 4 (20/10)"
},
{
"projectedStartDate":"2017-02-01T00:00:00",
"dateName":"February (6/10)"
},
{
"projectedStartDate":"2017-03-01T00:00:00",
"dateName":"March (0/2)"
},
{
"projectedStartDate":"2017-04-05T00:00:00",
"dateName":"April (2/5)"
}]
On the front end dropdown list, I want to be able to display the possibleDates that match closest to the 'projectedStart' date in $scope.data.
I am thinking of doing an angular foreach and looping through each projectedStart date in $scope.data and somehow compare it with each of the dates in $scope.possibleDates and updating $scope.Data's projectedStart with the closest match? Failing miserably so far.
Would greatly appreciate it if someone could point me in the right direction
Look's like you need to look into moment.js. It's a spectacular library for date parsing, formatting, and comparison.
You could put a watcher on possibleDates, and when they change you compute the list that you are after.
The list can be easily written with tools like moment and underscore.
const projectedDate = moment($scope.data[0].projectedStartDate);
const sortedPossibleDates = _.chain($scope.possibleDates)
.each((possibleDate) => {
possibleDate.diff = projectedDate.difference('seconds');
});
.sortBy('diff')
.value()
And voila, the sortedPossibleDates[0] is your best answer
A basic sort function should be able to order the possible dates by how close they are to the chosen start date. Just use new Date(ISOString) to get the numerical timestamps.
From: https://stackoverflow.com/a/11796365/6452040
If you put a function in script.js like this, which finds the closest date (before or after):
$scope.closestStartDate = function(startDate) {
var smallestDifferenceInDates = 1000000000000;//impossibly large value
var closestDateName = 'tmp';
var date = new Date(startDate);
for (var i = 0; i < Object.keys($scope.possibleDates).length; i++) {
var projectedDate = new Date($scope.possibleDates[i].projectedStartDate);
if (Math.abs(date.getTime() - projectedDate.getTime()) < smallestDifferenceInDates) {
smallestDifferenceInDates = Math.abs(date.getTime() - projectedDate.getTime());
closestDateName = $scope.possibleDates[i].dateName;
}
}
return closestDateName;
}
Then change your template code to this:
<tr ng-repeat="d in data">
<td>
{{ closestStartDate(d.projectedStart) }}
</td>
</tr>
You'll get the closest date from $scope.possibleDates (Math.abs() gets the distance between the two, if you want dates after you can leave that off).