最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 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
    };
});
发布评论

评论列表(0)

  1. 暂无评论