从JSON响应中提取后将对象推送到Javascript中的数组中
我试图将对象推入对象数组中。我编写的代码为数组中的所有索引重复最后一个索引值。任何关于我可能做错什么的帮助都会很好 代码:从JSON响应中提取后将对象推送到Javascript中的数组中,javascript,json,Javascript,Json,我试图将对象推入对象数组中。我编写的代码为数组中的所有索引重复最后一个索引值。任何关于我可能做错什么的帮助都会很好 代码: 如果comments确实是一个json对象,那么您可能更容易将响应映射到所需的数组,例如: const comments = response.items.map( comment => ({ commentId: comment.snippet.topLevelComment.id, text: comment.snippet.topLevelComme
如果comments确实是一个json对象,那么您可能更容易将响应映射到所需的数组,例如:
const comments = response.items.map( comment => ({
commentId: comment.snippet.topLevelComment.id,
text: comment.snippet.topLevelComment.snippet.textDisplay })
);
这将把响应
对象中的所有项
映射到新创建的注释数组中
const响应={
“种类”:“youtube#commentThreadListResponse”,
“etag”:“DDP0QPU15X6RNWI8DQDTP8SHFWK”,
“nextPageToken”:“QRTSL9PMHRQETI4QZLFDG9MDE44MFLONLHRS1HJS3FLSFGWS2FHM3GZAVZ0QZU5SS1KDVJUNKDXUWF6OU1QDM85OfJWDUJUZG4XR2DLRFR1BKLTTRUZWDFR1AWFTAXMDVtQUV0LURLEWLJFWODKHPN3RNUJDR1U=”,
“页面信息”:{
“总体结果”:4,
“结果每页”:4
},
“项目”:[
{
“种类”:“youtube#评论线程”,
“etag”:“7p4LroRwZP3DTt8DuMTZoPB8WFE”,
“id”:“UgyHbtFESKXeYdHptoN4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“topLevelComment”:{
“种类”:“youtube评论”,
“etag”:“l7TPuxvp3y-HuT7pUTFuilRsbCo”,
“id”:“UgyHbtFESKXeYdHptoN4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“文本显示”:“我来自遥远的未来,这仍然是有史以来最好的广告”,
“textOriginal”:“我来自遥远的未来,这仍然是有史以来最好的广告”,
“authorDisplayName”:“Max Haugen”,
“authorProfileImageUrl”:https://yt3.ggpht.com/a/AATXAJxruw_9hJGB8nS4D2yu2j8Z-SuZ9whhaeeF1w=s48-c-k-C0XFFFFFF-no-rj-mo“,
“AuthorChannel URL”:http://www.youtube.com/channel/UCmmmb4WuHKWaXOIM5W7CNPg",
“AuthorChannel ID”:{
“值”:“UCMMMB4WUKWAXOIM5W7CNPG”
},
“canRate”:没错,
“视图勘误”:“无”,
“likeCount”:0,
“发布日期”:“2020-05-27T15:18:30Z”,
“更新日期”:“2020-05-27T15:18:30Z”
}
},
“canReply”:没错,
“totalReplyCount”:0,
“isPublic”:正确
}
},
{
“种类”:“youtube#评论线程”,
“etag”:“gONUvfm77ZziX2qXYqMLG5ZrNY”,
“id”:“UgxBfriPiiexMxJSnht4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“topLevelComment”:{
“种类”:“youtube评论”,
“etag”:“6JGrrdeWCJstQ5-oN8Ll5uRxptw”,
“id”:“UgxBfriPiiexMxJSnht4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“textDisplay”:“我记得在学校做过类似的英语作业。真是一则广告”,
“textOriginal”:“我记得在学校做过类似的英语作业。真是一则广告”,
“authorDisplayName”:“驾驶频道”,
“authorProfileImageUrl”:https://yt3.ggpht.com/a/AATXAJzENHoGlkmsCBbxxfoYWlnbdXVJR_B-H_FuVQ=s48-c-k-C0XFFFFFF-no-rj-mo“,
“AuthorChannel URL”:http://www.youtube.com/channel/UCF2FnoRbunwPE17Z7sB-4FQ",
“AuthorChannel ID”:{
“值”:“UCF2FnoRbunwPE17Z7sB-4FQ”
},
“canRate”:没错,
“视图勘误”:“无”,
“likeCount”:0,
“发布日期”:“2020-05-27T14:27:17Z”,
“更新日期”:“2020-05-27T14:27:17Z”
}
},
“canReply”:没错,
“totalReplyCount”:0,
“isPublic”:正确
}
},
{
“种类”:“youtube#评论线程”,
“etag”:“2UZe_rxTkfikNASd6SJL_dIm3XU”,
“id”:“UgzdU_BTVF6gVN3UeTh4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“topLevelComment”:{
“种类”:“youtube评论”,
“etag”:“JRkp_uuugiugwrmx7ol-u jun2XvAU”,
“id”:“UgzdU_BTVF6gVN3UeTh4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“文本显示”:“仍然是有史以来最好的广告”,
“textOriginal”:“仍然是有史以来最好的广告”,
“authorDisplayName”:“Mika Baggins”,
“authorProfileImageUrl”:https://yt3.ggpht.com/a/AATXAJyUa2bJbuPGlW7fzBh8BmwdaHsjfxX2n7jUvA=s48-c-k-C0XFFFFFF-no-rj-mo“,
“AuthorChannel URL”:http://www.youtube.com/channel/UC8ydLvFnROzrIf_w60VNJuA",
“AuthorChannel ID”:{
“值”:“UC8ydLvFnROzrIf_w60VNJuA”
},
“canRate”:没错,
“视图勘误”:“无”,
“likeCount”:0,
“发布日期”:“2020-05-26T20:33:47Z”,
“更新日期”:“2020-05-26T20:33:47Z”
}
},
“canReply”:没错,
“totalReplyCount”:0,
“isPublic”:正确
}
},
{
“种类”:“youtube#评论线程”,
“etag”:“FIlRkfyxEh1b94jwmiCBBvw8Zrk”,
“id”:“UgyJJo90JtuEgL65VTh4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“topLevelComment”:{
“种类”:“youtube评论”,
“etag”:“fj-xjtD4g21_zw4XGt9sEIG3b_Q”,
“id”:“UgyJJo90JtuEgL65VTh4AaABAg”,
“片段”:{
“视频ID”:“M7FIvfx5J10”,
“文本显示”:“我非常喜欢它!!!!!!!”,
“textOriginal”:“我非常喜欢它!!!!!!!”,
“authorDisplayName”:“ББААСПАПАСУ”,
“authorProfileImageUrl”:https://yt3.ggpht.com/a/AATXAJyzs1NscDCXF_n7TnaAhkqEujQCQGHVjkabsg=s48-c-k-C0XFFFFFF-no-rj-mo“,
“AuthorChannel URL”:http://www.youtube.com/channel/UCzKl3D_2Mp25PQjVnI7zrqQ",
“AuthorChannel ID”:{
“值”:“UCzKl3D_2Mp25PQjVnI7zrqQ”
},
“canRate”:没错,
“视图勘误”:“无”,
“likeCount”:0,
“发布日期”:“2020-05-26T19:08:59Z”,
“更新日期”:“2020-05-26T19:08:59Z”
}
},
“canReply”:没错,
“totalReplyCount”:0,
{
"kind": "youtube#commentThreadListResponse",
"etag": "DdP0Qpu15x6rNWi8DqdtP8XHFWk",
"nextPageToken": "QURTSl9pMHRqeTI4QzlFdG9mdE44MFloNlhrS1hJS3FLSFgwS2FhM3gzaVZ0QzU5SS1KdVBJNkdXUWF6OU1Qdm85OFJwdUJuZG4xR2dLRFR1bkltWTRuZVdfR1NaaWFtaXMxVDVtQUxCQUV0LURLRlEwMnJFWVVodkhpN3RnUUJDR1U=",
"pageInfo": {
"totalResults": 4,
"resultsPerPage": 4
},
"items": [
{
"kind": "youtube#commentThread",
"etag": "7p4LroRwZP3DTt8DuMTZoPB8WFE",
"id": "UgyHbtFESKXeYdHptoN4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "l7TPuxvp3y-HuT7pUTFuilRsbCo",
"id": "UgyHbtFESKXeYdHptoN4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"textDisplay": "I come from the distant future and this is still the best commercial ever made",
"textOriginal": "I come from the distant future and this is still the best commercial ever made",
"authorDisplayName": "Max Haugen",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJxruw_9hJGB8nS4D2yu2j8Z-SuZ9whhaeeF1w=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCmmmb4WuHKWaXOIM5W7CNPg",
"authorChannelId": {
"value": "UCmmmb4WuHKWaXOIM5W7CNPg"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-05-27T15:18:30Z",
"updatedAt": "2020-05-27T15:18:30Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "_gONUvfm77ZziX2qXYqMLG5ZrNY",
"id": "UgxBfriPiiexMxJSnht4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "6JGrrdeWCJstQ5-oN8Ll5uRxptw",
"id": "UgxBfriPiiexMxJSnht4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"textDisplay": "I remember doing something similar for an English assignment back in school. What an ad",
"textOriginal": "I remember doing something similar for an English assignment back in school. What an ad",
"authorDisplayName": "Driving Channel",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJzENHoGlkmsCBbxxfoYWlnbdXVJR_B-H_FuVQ=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCF2FnoRbunwPE17Z7sB-4FQ",
"authorChannelId": {
"value": "UCF2FnoRbunwPE17Z7sB-4FQ"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-05-27T14:27:17Z",
"updatedAt": "2020-05-27T14:27:17Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "2UZe_rxTkfikNASd6SJL_dIm3XU",
"id": "UgzdU_BTVF6gVN3UeTh4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "JRkp__giuGwRMX7OL-_jun2XvAU",
"id": "UgzdU_BTVF6gVN3UeTh4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"textDisplay": "still the best ad ever created",
"textOriginal": "still the best ad ever created",
"authorDisplayName": "Mika Baggins",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJyUa2bJbuPGlW7fzBh8BmwdaHsjfxX2n7jUvA=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UC8ydLvFnROzrIf_w60VNJuA",
"authorChannelId": {
"value": "UC8ydLvFnROzrIf_w60VNJuA"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-05-26T20:33:47Z",
"updatedAt": "2020-05-26T20:33:47Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
},
{
"kind": "youtube#commentThread",
"etag": "FIlRkfyxEh1b94jwmiCBBvw8Zrk",
"id": "UgyJJo90JtuEgL65VTh4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "fj-xjtD4g21_zw4XGt9sEIG3b_Q",
"id": "UgyJJo90JtuEgL65VTh4AaABAg",
"snippet": {
"videoId": "M7FIvfx5J10",
"textDisplay": "I like it tooooooo much !!!!!!!",
"textOriginal": "I like it tooooooo much !!!!!!!",
"authorDisplayName": "В НАТУРАШКУ",
"authorProfileImageUrl": "https://yt3.ggpht.com/a/AATXAJyzs1NscDCXF_n7TnaAhkqEujQCQGHVjkabsg=s48-c-k-c0xffffffff-no-rj-mo",
"authorChannelUrl": "http://www.youtube.com/channel/UCzKl3D_2Mp25PQjVnI7zrqQ",
"authorChannelId": {
"value": "UCzKl3D_2Mp25PQjVnI7zrqQ"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 0,
"publishedAt": "2020-05-26T19:08:59Z",
"updatedAt": "2020-05-26T19:08:59Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
}
]
}
const comments = response.items.map( comment => ({
commentId: comment.snippet.topLevelComment.id,
text: comment.snippet.topLevelComment.snippet.textDisplay })
);