How to iterate over all the properties of an object, convert them in some way and return the converted object?

there is an object like that

"object": { "user": "admin", "date": "14877890", "last": "SKIPPED" } 

You need to convert date to date and get a new object.

  • May let copy = Object.assign({}, object); copy.date = функция_для_преобразования_даты(copy.date); let copy = Object.assign({}, object); copy.date = функция_для_преобразования_даты(copy.date); ? - diraria
  • @diraria, assign Only the top level copies, if there is a nested object, then when its fields change, changes will be noticeable in both objects - Grundy
  • @Grundy, yes, exactly. - diraria
  • @Grundy, is there any question about deep copying on ruSO? - diraria
  • one
    @diraria, yes, there is: copying an object with a subsequent change - Grundy

1 answer 1

Here is an example with the map function for passing through an object, and converting the timestamp to date

 var object = { "user": "admin", "date": "14877890", "last": "SKIPPED" } function timestamp2date(timestamp) { var theDate = new Date(timestamp * 1000); return theDate.toGMTString(); } Object.keys(object).map(function(objectKey, index) { var value = object[objectKey]; if(objectKey == 'date'){ console.log(timestamp2date(value)); } }); 

  • But how can I create a copy of an object through the spread operator and change only one property in it, without going through them all? without if - werty
  • @werty ask a full question and I will answer you - Raz Galstyan
  • I created a separate question ru.stackoverflow.com/questions/688029/… - werty