[英]Trying to create nested array within an Object Javascript
我創建了一個循環,看來我無意間重寫了一個對象屬性。
我的代碼如下:
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
值。 在我看來,這應該可以很好地工作,但是我認為我缺少了一些東西。
有什么建議么?
我已經推斷出有關您代碼周圍環境的一些信息,但是這個小提琴對我有用:
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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.