Reputation: 149
I am having some trouble figuring this out. I have a Json response:
[
{
"id": 82797,
"name": "pom-ta-all",
"date": "2016-06-26T11:57:53+0000",
"creationDate": "2016-06-10T14:15:00+0000",
"version": "1.0.0",
},
{
"id": 80926,
"name": "pom-evnthdl-wln-all",
"creationDate": "2016-06-03T17:44:20+0000",
"version": "1.1.0",
}...
that I get from rest api using Jquery Ajax. How do I, in the Ajax call, add the current date to each element so that it looks like so:
[
{
"id": 82797,
"name": "pom-ta-all",
"date": "2016-06-26T11:57:53+0000",
"creationDate": "2016-06-10T14:15:00+0000",
"version": "1.0.0",
"currentDate": "2016-06-28"
},...
Can I use .append()
? if so, how? I am not very familiar with Ajax and JavaScript in general. It would be best if the format was like so:
var job = $.ajax({
url: url,
dataType: 'json',
data: data,
success: #insert magic here, <---
error: function()
});
Thanks for your thoughts
Upvotes: 1
Views: 1205
Reputation: 626
Your success function
...
success: function(json) {
$.each(json, function(idx) {
json[idx].currentDate = new Date();
});
}
...
Working Example, based on your inputData
var json = [
{
"id": 82797,
"name": "pom-ta-all",
"date": "2016-06-26T11:57:53+0000",
"creationDate": "2016-06-10T14:15:00+0000",
"version": "1.0.0",
},
{
"id": 80926,
"name": "pom-evnthdl-wln-all",
"creationDate": "2016-06-03T17:44:20+0000",
"version": "1.1.0",
}];
$.each(json, function(idx) {
json[idx].currentDate = new Date();
});
console.log(json);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
How you get current date formatted with JS see suggestion from other guy:
How do I get the current date in JavaScript?
Upvotes: 2