4

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());
2
  • 1
    Do you want to mutate the objects or create new ones? Commented Jun 10, 2015 at 0:02
  • I want new ones. The answer below did the trick. Thank you. Commented Jun 10, 2015 at 0:07

1 Answer 1

5

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
    };
});
Sign up to request clarification or add additional context in comments.

2 Comments

Rather "build the new value" instead of "make changes" :-)
@Bergi Yes, I'd like to have the flexibility of both the firstArray and the newArray. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.