我创建了一个循环,看起来我无意中重写了一个对象属性。
我的代码如下:
Object.keys(existingComments1).forEach(function(key) {
console.log(existingComments1[key]['sectionId']);
comment.sectionId = existingComments1[key]['sectionId'];
existingComments.push(comment);
});
[{
"sectionId": "3",
"comments": [
{
"id": 66,
"authorAvatarUrl": "support/images/clay_davis.png",
"authorName": "Senator Clay Davis",
"authorId": 3,
"comment": "These Side Comments are incredible. Sssshhhiiiiieeeee."
}
]
}];
当我查看控制台时,我一遍又一遍地具有相同的sectionId
值。在我看来,这应该是完美的,但我想我错过了一些东西。
有什么建议吗?
答案 0 :(得分:0)
我已经推断了一些关于代码环境的事情,但这个小提琴对我有用:
existingComments = []
existingComments1 = { foo: {sectionId: 1}, bar: {sectionId:3} }
Object.keys(existingComments1).forEach(function(key) {
console.log(existingComments1[key]['sectionId']);
comment = {}
comment.sectionId = existingComments1[key]['sectionId'];
existingComments.push(comment);
});