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

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

    The issue here is that you're returning the array from the callback of the then function, which doesn't do anything. You want to pass the callback into the then function like so:

    function doSomething(tasks) {
      let results = tasks.map(task => task.start_date = task.start_date.format("YYYY-MM-DD"));
      return results;
    }
    
    function getAllTasks(callBack) {
      models.gantt_tasks.findAll()
      .then(callback)
      .catch(function(e) {
        console.error(e);
        return e;
      });
    }
    
    var results = getAllTasks(doSomething);
    

提交回复
热议问题