D
D
DimaPolishuk2016-08-03 19:38:58
JavaScript
DimaPolishuk, 2016-08-03 19:38:58

How to iterate through an array of objects, select a certain property, change it, and return the new value of that property to the array?

there is an array of objects

var tasks = [{
        name: "Задача № 1",
        selectedProject: 'select project',
        dateBegin: new Date('07 24, 2016 19:58:49'),
        dateFinish: new Date('07 24, 2016 19:58:59')
      }, {
        name: "Задача № 2",
        selectedProject: 'timer',
        dateBegin: new Date('07 25, 2016 19:58:30'),
        dateFinish: new Date('07 25, 2016 19:59:15')
      }]

I save it in local Storage on a certain action
localStorage.setItem("tasks", JSON.stringify(newTasks));

on page load i fetch this array with LS
var newTasks = JSON.parse(localStorage.getItem("tasks"))

now I need to run through this array, parse the dates and write them to the array, I do it like this
for(var i=0; i < newTasks.length; i++)
   new Date(Date.parse(newTasks[i].dateBegin));
  
    for(var i=0; i < newTasks.length; i++)
 new Date(Date.parse(newTasks[i].dateFinish));

How can I return already parsed dates to an array???

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
theWaR_13, 2016-08-03
@DimaPolishuk

Well, it became

for(var i=0; i < newTasks.length; i++)
   newTasks[i].dateBegin = new Date(Date.parse(newTasks[i].dateBegin));

A
Alexander Mylchenko, 2016-08-04
@ERrorMAKros

Array. map

var names = ['HTML', 'CSS', 'JavaScript'];

var nameLengths = names.map(function(name) {
  return name.length;
});

// получили массив с длинами
alert( nameLengths ); // 4,3,10

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question