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

后端 未结 6 1432
生来不讨喜
生来不讨喜 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:34

    You stay focused on simplicity of arrow functions. You can use Brackets {} and return the task object after change start_date. See:

    //Get all the Tasks
    function getAllTasks() {
      return models.gantt_tasks.findAll()
      .then(tasks => {
    
        let results = tasks.map(task => {
          task.start_date = task.start_date.format("YYYY-MM-DD");
          return task;
        });
        return results;
      })
      .catch(e => {
        console.error(e);
        return e;
      });
    }
    
    

    Nothing block you from use brackets and return on arrow functions.

提交回复
热议问题