How can I use angular-moment to set the format? - javascript

I have a date, that look like this(from json):
detailAssets.CONTRACT_END_DATE = '201811';
and I am using angular-moment to format to date, but results are not correct
<tr ng-repeat="assetsdetail in detailAssets | filter:profileId">
<td>{{assetsdetail.CONTRACT_END_DATE | amDateFormat : 'YYYY-MM'}}</td> // shows 1970-01
</tr>
how can I set the format using angular-moment, like this`? :
moment(detailAssets.CONTRACT_END_DATE, 'YYYYMM')
because the above method worked in console and showed the right results : 2018-11

Is detailAssets an array or object?
If it's an object, you should not use ng-repeat on an object.

Related

Read data from JSON with multiple JSON objects in order to be displayed

I have the following JSON object that contains multiple JSON objects.
I want to read and display the following elements 1) CampaignName 2) Start date 3) End date
I have written the following the following code which produces error to the browser console. So, the items are not displayed.
the produced error at the browser console is:
I assume I do not access the elements as appropriate.
How shall I do it ?
You are using the .each function that iterate over each object... each object has the properties that you need. Each of these values you are iterating over is an object and not an array
Use v.Campaign.CampaignName instead of accessing the index first
the v that you are trying to access already is the date on a single JSON object so there is no need of passing the count param. I would suggest you refactoring your code to something like:
data.forEach(({ Campaign: {CampaignName, StartDate, EndDate } }) => {
events.push({
title: CampaignName,
title: moment(StartDate),
title: moment(EndDate),
})
})

Iterating over an object using mustache when you don't know the key name

I'm using mustache with javascript to try and loop through an object to print out the keys and values. The problem is that the names of the keys can be anything, and all the documentation I've seen just shows how to display the key when you know its name. Does anyone know how to do this?
My object looks like:
var obj1 = {
random1: 10
random4: 12
random9: 15
}
I've tried doing something like this:
{{#obj1}}
<tr>
<td>{{key}}:{{Value}}</td>
</tr>
{{/obj1}}
But that hasn't worked.

Time string to date object conversion js angularJS

I am getting following string from REST api,
20160220
I want to make it 20/02/2016
I am using angularJS. So I will require a filter.
I have tried following
app.filter('myDateFilter', function() {
return function(input) {
var st = input;
var pattern = /(\d{4})(\d{2})(\d{2})/;
var date = new Date(st.replace(pattern, '$1-$2-$3'));
return date;
}
});
And in html I have used
<td>
{{t["due-date"] | myDateFilter}}
</td>
This returns 2016-02-20T00:00:00.000Z
Regular expression issue? Can you kindly give me proper code which should have been used instead to generate 20/02/2016.
Naively converting a Date into a string results in the output you are seeing:
console.log(new Date()) // "2016-09-02T15:19:07.921Z"
Instead, make sure you format the date into a string manually before returning it. E.g. toLocaleDateString() converts the Date into a string, taking into account the browser's locale:
console.log(new Date().toLocaleDateString()) // "09/02/2016"
What you are doing is converting a String to a Date() object, which seems right to me. If you try to show your Date() object in your view, what you get is the default date format.
In order to customize the format in which your Date() object is showing, you need to chain another filter to your custom filter. In this case you need to use date filter: https://docs.angularjs.org/api/ng/filter/date
You would only need to add it to your template, like this:
<td>
{{t["due-date"] | myDateFilter | date : 'dd/MM/yyyy'}}
</td>
This way date filter will take as input the Date() object returned by your custom myDateFilter filter and produce a String representing that object as output.
This is a nice example of how angular filters and filter chaining are supposed to be used.

how to parse this JSON data {"A":"[[a,b],[c,d]]"}

I want to parse this format of data by JSON --- {"A":"[[a,b],[c,d]]"} . I want to retrieve values a,b,c,d separately.
IF I DO alert(JSON.stringify(data)); This shows me proper response.
I Tried following things to parse but didnt work.
alert(data.A[0]);
alert(data.[0]);
alert(data.A);
data is JSONOBJECT HERE.
1 and 2 does not work at all.
3 at least show me undefined. Please someone tell me how i can retrieve this format.
Try this and check console
data = {"A":"[[a,b],[c,d]]"} ;
console.log(data.A);
"[[a,b],[c,d]]"
This is not javascript object . Its a string
If you want to use it as a javascript collection
Use as follows
data = {"A":[["a","b"],["c","d"]]} ;
console.log(data.A[0]); //=>["a", "b"]
console.log(data.A[0]); //=>["c","d"]
change your json object by data={A:[['a','b'],['c','d']]} . you can now access data.A[0][0], data.A[0][1]...

Angular - orderBy string as number

Im using youtube json api and it returns the viewcount as a string, but i need to sort it as an number. How could i do this without going thru the whole model and converting strings to ints?
json snippet
yt$hd: Object
yt$statistics: Object
favoriteCount: "0"
viewCount: "1443"
__proto__: Object
and im invoiking sorting like
orderBy:'yt$statistics.viewCount'
Not that firmilar with Angular, but I'd assume that orderBy parameter can take a function in place of a string like Lo-Dash does with its `sortBy() function. In which case you'd probably want something like this:
orderBy: function(obj){
return ~~obj.yt$statistics.viewCount
}

Categories