Map over an object and change one properties value using native JS

后端 未结 6 1438
生来不讨喜
生来不讨喜 2021-02-06 22:50

I want to be able to return a result set of data and just change the formatting of the date field to something more readable leaving all the other data intact. I would prefer t

6条回答
  •  一个人的身影
    2021-02-06 23:24

    Both ways worked for me. I think for testing I will use the forEach() until I need to duplicate it and then I will create the separate function.

    This is what I ended up with.

    let moment = require('moment');
    
    // Get all the Tasks
    function getAllTasks() {
     return models.gantt_tasks.findAll()
     .then(function(tasks) {
       tasks.forEach( task => task.start_date = moment(task.start_date).format("YYYY-MM-DD"));
       return tasks;
     })
     .catch(function(e) {
       console.error(e);
       return e;
     });
    }
    
    router.get('/data', (req, res) =>
     getAllTasks()
      .then((tasks) => res.json({ data: tasks }))
    )
    

    I ended up needing the Moment library to convert my dates as Sequelize wasn't let me easily manipulate the dates.

提交回复
热议问题