javascript – How to iterate over all the properties of an object, transform them in some way and return the transformed object?

Question:

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

there is such an object

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

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

Answer:

Here's an example with a map function to traverse an object, and convert 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));
    }
});
Scroll to Top