-1

I have an application, where I use MongoDB, Express and MongoDB. On server side I received data by using find in such look - array of objects(JSON), which is named upProbations. For example, I find information about Max:

[        
  {
        Name: 'Max',
        DateOne: 2019-01-01T00:00:00.000Z,
        DateTwo: 2019-04-01T00:00:00.000Z,
        Info: 'Cheerful'
  }
]

I want to change information a bit - fields DateOne and DateTwo. They have both types Date and I want to leave only 2019-01-01 and same for DateTwo, so i don't need time(hour, minute, second). The result should look like this:

[        
  {
        Name: 'Peter',
        DateOne: 2019-01-01,
        DateTwo: 2019-04-01,
        Info: 'Cheerful'
  }
]

This result I will futher use for antoher calculations. I have tried such code, but it's not working:

upProbations = upProbations.map(function(a) {
    a.DateOne = a.DateOne.toISOString().replace('T', ' ').substr(0, 10);
return a;
})
console.log(upProbations);

In which way I can do this?

Andy
  • 79
  • 7
  • 1
    So you want to turn them from fields of type Date to fields of type string, where the string only shows the year-month-day? Note that the `startProb` property does not exist on those objects. Did you mean `DateOne` and `DateTwo`? – CertainPerformance Jun 17 '22 at 22:57
  • @CertainPerformance, yeah, I want exactly what you said and I mean DateOne and DateTwo, I will edit it) – Andy Jun 17 '22 at 23:50
  • Your code works, you just need to access the proper property names on the object... – CertainPerformance Jun 17 '22 at 23:53
  • @CertainPerformance seeing results in console.log give me the same result without changing anything – Andy Jun 18 '22 at 07:36

1 Answers1

-1

let upProbations = [
  {
    Name: "Max",
    DateOne: 2019-01-01T00:00:00.000Z,
    DateTwo: 2019-04-01T00:00:00.000Z,
    Info: "Cheerful",
  },
];

upProbations = upProbations.map((eachObject) => {
  const { DateOne, DateTwo, Name, Info } = eachObject;
  return {
    Name,
    DateOne: DateOne.toString().split("T")[0],
    DateTwo: DateTwo.toString().split("T")[0],
    Info,
  };
});

console.log(upProbations);
DanteDX
  • 1,059
  • 7
  • 12