-1

I'm writing a js code that takes in a JSON of string arrays, that also has dates formatted as strings in it. Below is a sample code of the same.

var prods = [
  {
    "id": "56e535de-319f-4612-be83-3084a060b77e",
    "createdDate": "2021-04-28T11:34:54.975",
    "modifiedDate": "2021-04-28T11:34:54.976"
  },
  {
    "id": "55753a5d-377d-4038-baf0-5ecbf620601a",
    "createdDate": "2021-04-27T16:22:02.621",
    "modifiedDate": "2021-04-27T16:22:02.621"
  },
  {
    "id": "e9593d91-d884-40e8-8239-5f69794f2b3a",
    "createdDate": "2021-04-27T15:29:55.737",
    "modifiedDate": "2021-04-27T15:29:55.737"
  }
];


prods = prods.map(function(a) {
  a.createdDate = Date.parse(a.createdDate);
  return a;
});


console.log(prods)

Here I'm able to convert for createdDate. I want to know how I can do for both createdDate as well as modifiedDate by looping over the JSON.

Thanks

user3872094
  • 3,269
  • 8
  • 33
  • 71
  • What's the difference between createdDate and modifiedDate? – Jonas Wilms Apr 28 '21 at 14:36
  • 1
    What is keeping you from adding a `a.modifiedDate = ` line to your map function? (also: JSON is a text format. What you have there is simply an array of objects. There's no JSON anywhere in your code) –  Apr 28 '21 at 14:37
  • https://jsfiddle.net/pf9167t3/2/ just return an object from the map maybe it will grow use rest to add the rest of the object //when you have support for spread operator – cannap Apr 28 '21 at 14:39

1 Answers1

0

something like that ?

const prods = 
  [ { id           : '56e535de-319f-4612-be83-3084a060b77e'
    , createdDate  : '2021-04-28T11:34:54.975'
    , modifiedDate : '2021-04-28T11:34:54.976'
    } 
  , { id           : '55753a5d-377d-4038-baf0-5ecbf620601a'
    , createdDate  : '2021-04-27T16:22:02.621'
    , modifiedDate : '2021-04-27T16:22:02.621'
    } 
  , { id           : 'e9593d91-d884-40e8-8239-5f69794f2b3a'
    , createdDate  : '2021-04-27T15:29:55.737'
    , modifiedDate : '2021-04-27T15:29:55.737'
    } 
  ]

prods.forEach((el,i,arr)=>
  {
  arr[i].createdDate = Date.parse(el.createdDate)
  arr[i].modifiedDate = Date.parse(el.modifiedDate)
  })

console.log(prods)
.as-console-wrapper {max-height: 100%!important;top:0;}
Mister Jojo
  • 20,093
  • 6
  • 21
  • 40