javascript - Map over an object and change one properties value using native JS -
i want able return result set of data , change formatting of date field more readable leaving other data intact. prefer without third party library.
here have far:
//get tasks function getalltasks() { return models.gantt_tasks.findall() .then(function(tasks){ let results = tasks.map(task => task.start_date = task.start_date.format("yyyy-mm-dd")); return results; }) .catch(function(e) { console.error(e); return e; }); }
and map complete before returns data.
.map()
isn't usual way modify properties existing array of objects. can that, it's not .map()
used for. instead, it's way create new array. .map()
callback returns value, , returned values pushed new array, .map()
returns.
your .map()
callback implicitly returning task.start_date
after assigning formatted date it. means you're creating array of formatted dates only, without rest of original task
objects. if wanted use .map()
here need return task
after making assignment task.start_date
.
but instead may want use .foreach()
in .then()
callback. method made job: iterates on array , lets modify each element in place:
tasks.foreach( task => task.start_date = task.start_date.format("yyyy-mm-dd") ); return tasks;
also, dustin mentioned, returning value .then()
may not want here. safest bet tasks
right there, or call function , pass tasks
argument. (i'm little rusty on promises, though...)
Comments
Post a Comment