Stuart Brown
Stuart Brown

Reputation: 987

Modifying JavaScript array properties

I'm using Express and have an array of objects. I would like to modify the created property (I am using dateFormat package) for presentation.

The array is the result of a mongo query and is assigned to a variable stories and looks like:

[
    { public: false,
      keyStageLevel: [ 'ks1' ],
      created: 2018-03-25T13:01:30.809Z,
      _id: 5ab79daafa832035b416536f,
      slug: 'goldilocks',
      name: 'Goldilocks',
      comments: null,
      id: '5ab79daafa832035b416536f' },
    { public: false,
      keyStageLevel: [ 'ks1' ],
     created: 2018-03-25T13:07:22.421Z,
     _id: 5ab79f0afa832035b4165370,
     slug: 'cinderella',
     name: 'Cinderella',    
     comments: null,
     id: '5ab79f0afa832035b4165370' },
   ..
]

If I execute

stories.map(function(story){
    story.created = dateFormat(story.created, "dddd, mmmm dS, yyyy, h:MM:ss TT")
    console.log('AFTER TRANSFORM '+story.created);
});

The console.log shows the date format I want but the story.created values in the stories array is unmodified. What is the correct way (ES5 if possible, I'm learning and I find it easier to understand what is going on) to reformat the created property in the stories array?

Edit - showing the query and use of .map and .foreach both of which leave `stories' unmodified

let stories = await Story.find({$or:[{'group': {$in:group}}, 
  {'public':true} ]})
    .populate('group')
    .populate('createdBy');
console.log('BEFORE TRANSFORM '+stories);
// console.log(stories);
// stories.map(function(story){
//     story.created = dateFormat(story.created, "dddd, mmmm dS, yyyy, h:MM:ss TT")
//     console.log('AFTER TRANSFORM '+story.created);
// });
stories.forEach(story => {
    story.created = dateFormat(story.created, "dddd, mmmm dS, yyyy, h:MM:ss TT")
});
console.log('AFTER TRANSFORM '+stories);

Upvotes: 0

Views: 62

Answers (2)

Rahul Desai
Rahul Desai

Reputation: 15491

You may wanna consider forEach():

stories.forEach(story => {
    story.created = dateFormat(story.created, "dddd, mmmm dS, yyyy, h:MM:ss TT")
});

Docs: Array.prototype.forEach() - JavaScript | MDN

Upvotes: -1

Scott Sauyet
Scott Sauyet

Reputation: 50787

As others note, you're misusing map. My choice, though, would be to use map properly, and create a new array from your old one. I think it's much easier to reason about data that is not mutated. Here's one way to do that:

const stories = [{"_id": "5ab79daafa832035b416536f", "comments": null, "created": "2018-03-25T13:01:30.809Z", "id": "5ab79daafa832035b416536f", "keyStageLevel": ["ks1"], "name": "Goldilocks", "public": false, "slug": "goldilocks"}, {"_id": "5ab79f0afa832035b4165370", "comments": null, "created": "2018-03-25T13:07:22.421Z", "id": "5ab79f0afa832035b4165370", "keyStageLevel": ["ks1"], "name": "Cinderella", "public": false, "slug": "cinderella"}]

const dateFormat = (d) => d.slice(0, 10) // dummy

const updatedStories = 
    stories.map(story => ({...story, created: dateFormat(story.created)}))

console.log(updatedStories)

If that ES6 bothers you, then you could replace it with

stories.map(story => Object.assign({}, story, {created: dateFormat(story.created)}))

Upvotes: 2

Related Questions