Change value of each property in array of objects in javascript - map() method?

6k Views Asked by At

I have this array of objects:

var firstArray = [{created:"01/01/2015 03:24:33 AM", lat:"123"}, {created:"01/02/2015 03:24:33 AM", lat:"123"}];

I want a new array of objects with the datetimes converted to milliseconds:

var newArray = [{created:1420070400000, lat:"123"}, {created:1420156800000, lat:"123"}];

How do I change the values of the first property in each object? I'm trying .map(), but maybe that isn't right. What am I missing? I've tried a bunch of things, but just wrote it in quotes in the code below to try and convey what I'm after. Thanks.

var newArray = firstArray.map( "the value of the first property in each object" (Date.parse());
1

There are 1 best solutions below

2
On

The argument to .map should be a function where you make your appropriate changes.

newArray = firstArray.map(function (item) {
    return {
        created: Date.parse(item.created),
        lat: item.lat
    };
});