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
1 Answer
Reset to default 12The 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
版权声明:本文标题:javascript - Adding Properties in a forEach Loop in Mongoose - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741234266a2362692.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论