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());
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());
Share
Improve this question
asked Jun 9, 2015 at 23:59
user4534236user4534236
2
- 1 Do you want to mutate the objects or create new ones? – Bergi Commented Jun 10, 2015 at 0:02
- I want new ones. The answer below did the trick. Thank you. – user4534236 Commented Jun 10, 2015 at 0:07
1 Answer
Reset to default 5The 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
};
});