admin管理员组

文章数量:1278910

lets say each post in my posts array has two properties name and number. so its something like

var posts = [{name:"hey", number: 1}, {name:"foo", number:"2"}]

Javascript allows me to change these properties in foreach loop like this:

posts.forEach(function(post){
    post.name = "bar"
});

and my array bees:

var posts = [{name:"bar", number: 1}, {name:"bar", number:"2"}]

but it doesnt allow me add a new property like:

posts.forEach(function(post){
    post.adress = "bar"
});

my object stays the same. Is there a way to add properties in a foreach loop in javascipt

edit:

this is happening using mongoose inside a callback..

  Post.pagination(options, function(err, posts) {
    if (err) console.log(err)
    posts.forEach(function(post){
      post.votetype = 1;
    });
    console.log(posts);
    res.send({ posts : posts  })
  })

after this votetype property is not added

lets say each post in my posts array has two properties name and number. so its something like

var posts = [{name:"hey", number: 1}, {name:"foo", number:"2"}]

Javascript allows me to change these properties in foreach loop like this:

posts.forEach(function(post){
    post.name = "bar"
});

and my array bees:

var posts = [{name:"bar", number: 1}, {name:"bar", number:"2"}]

but it doesnt allow me add a new property like:

posts.forEach(function(post){
    post.adress = "bar"
});

my object stays the same. Is there a way to add properties in a foreach loop in javascipt

edit:

this is happening using mongoose inside a callback..

  Post.pagination(options, function(err, posts) {
    if (err) console.log(err)
    posts.forEach(function(post){
      post.votetype = 1;
    });
    console.log(posts);
    res.send({ posts : posts  })
  })

after this votetype property is not added

Share Improve this question edited Jul 31, 2013 at 1:08 s_curry_s asked Jul 31, 2013 at 1:00 s_curry_ss_curry_s 3,4329 gold badges33 silver badges49 bronze badges 4
  • 2 Are you sure? I get the result you desire: jsfiddle/dAHJz . Look in the console and expand the array (and its objects) and you'll see the address property was added to each – Ian Commented Jul 31, 2013 at 1:02
  • Is this in the browser or on the server side using Node.js with Mongoose (or something similar)? – whirlwin Commented Jul 31, 2013 at 1:05
  • 1 using mongoose.. I guess I oversimplified my question – s_curry_s Commented Jul 31, 2013 at 1:06
  • 1 Then you will need to call .toObject() on the particular object, and you will then get an object which you can modify. – whirlwin Commented Jul 31, 2013 at 1:06
Add a ment  | 

1 Answer 1

Reset to default 12

The problem is that data returned from Mongoose is immutable. The code below is untested but should give you a hint on how to make the data mutable and modify it.

The key point is calling toObject() on the Mongoose document object you wish to modify.

Post.pagination(options, function(err, posts) {
  if (err) console.log(err);

  var resultPosts = posts.map(function(post) {
    var tmpPost = post.toObject();

    // Add properties...
    tmpPost.votetype = 1;

    return tmpPost;
  });

  console.log(resultPosts);
  res.send({ posts : resultPosts  });
});

本文标签: javascriptAdding Properties in a forEach Loop in MongooseStack Overflow