gpt4 book ai didi

javascript - 如何在 Javascript 对象数组的最后一个对象之后附加几个新对象?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:45:25 24 4
gpt4 key购买 nike

我正在制作一个由 wordpress 组成的新闻网站的移动应用程序(使用 ionic 框架)。json 文件是从站点获取的。我必须从 wordpress 网站获取相同的重复 json 数据和一些新帖子,以便我的应用程序在用户继续滚动时显示新消息。所以我必须将这些新的几个帖子对象添加/附加到以前的“帖子”对象数组中。这是常规的 wordpress posts json 文件对象的样子。

{
"status": "ok",
"count": 2,
"count_total": 4231,
"pages": 2116,
"posts": [
{
"id": 62296,
"type": "post",
"slug": "any slug",
"url": "any url",
"status": "publish",
"title": "any title",
"title_plain": "any title",
"content": "any content",
"excerpt": "any excerpt",
"date": "2015-02-26 02:08:57",
"modified": "2015-02-26 02:09:59",
"categories": [
{
"id": 1015,
"slug": "any slug",
"title": "any title",
"description": "",
"parent": 0,
"post_count": 7
}
],
"tags": [
{
"id": 1180,
"slug": "any slug",
"title": "any title",
"description": "",
"post_count": 6
},
{
"id": 1179,
"slug": "religion",
"title": "religion",
"description": "",
"post_count": 5
},
{
"id": 1209,
"slug": "any slug",
"title": "any title",
"description": "",
"post_count": 4
}
],
"author": {
"id": 1,
"slug": "newsdesk",
"name": "NewsDesk",
"first_name": "",
"last_name": "",
"nickname": "NewsDesk",
"url": "",
"description": ""
},
"comments": [],
"attachments": [
{
"id": 44930,
"url": "anything appropriate",
"slug": "anything appropriate",
"title": "anything appropriate",
"description": "",
"caption": "",
"parent": 62296,
"mime_type": "image/jpeg",
"images": {
"full": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"thumbnail": {
"url": "anything appropriate",
"width": 150,
"height": 147
},
"medium": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"tie-small": {
"url": "anything appropriate",
"width": 55,
"height": 55
},
"tie-medium": {
"url": "anything appropriate",
"width": 272,
"height": 125
},
"tie-large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"big-slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
}
}
}
],
"comment_count": 0,
"comment_status": "open",
"thumbnail": "anything appropriate": {
"views": [
"2"
],
"taq_review_button_text": [
""
],
"taq_review_button_size": [
"medium"
],
"taq_review_button_shape": [
"square"
],
"taq_review_button_color": [
"#c7c7c7"
],
"taq_button_icon": [
"fa fa-check"
],
"taq_review_button_type": [
"flat"
],
"taq_review_button_url": [
""
],
"taq_review_title": [
""
],
"taq_review_position": [
""
],
"taq_review_style": [
"stars"
],
"taq_review_summary": [
""
],
"taq_review_total": [
""
],
"tie_hide_meta": [
""
],
"tie_hide_author": [
""
],
"tie_hide_share": [
""
],
"tie_hide_related": [
""
],
"tie_hide_check_also": [
""
],
"tie_sidebar_pos": [
"default"
],
"tie_sidebar_post": [
"category lifestyle"
],
"tie_post_head": [
"none"
],
"tie_post_slider": [
"59602"
],
"tie_googlemap_url": [
""
],
"tie_video_url": [
""
],
"tie_video_self": [
""
],
"tie_embed_code": [
""
],
"tie_audio_m4a": [
""
],
"tie_audio_mp3": [
""
],
"tie_audio_oga": [
""
],
"tie_audio_soundcloud": [
""
],
"tie_banner_above": [
""
],
"tie_banner_below": [
""
],
"tie_posts_num": [
""
],
"post_color": [
""
],
"post_background": [
"anything appropriate"
],
"tie_views": [
"2"
]
},
"thumbnail_size": "thumbnail",
"thumbnail_images": {
"full": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"thumbnail": {
"url": "anything appropriate",
"width": 150,
"height": 147
},
"medium": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"tie-small": {
"url": "anything appropriate",
"width": 55,
"height": 55
},
"tie-medium": {
"url": "anything appropriate",
"width": 272,
"height": 125
},
"tie-large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"big-slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
}
}
},
{
//This is another "posts" object
}
]
}

我对javascript不是很熟悉。因此,将新获取的帖子添加到同一个“帖子”数组中让我感到非常痛苦。

我可以将“posts”数组保存到我的应用程序中的一个变量中。但是不能在最后一个帖子对象之后将新获取的对象追加到“帖子”数组中。

最佳答案

var news = {
"status": "ok",
"count": 2,
"count_total": 4231,
"pages": 2116,
"posts": [{
"id": 62296,
"type": "post",
"slug": "any slug",
"url": "any url",
"status": "publish",
"title": "any title",
"title_plain": "any title",
"content": "any content",
"excerpt": "any excerpt",
"date": "2015-02-26 02:08:57",
"modified": "2015-02-26 02:09:59",
"categories": [{
"id": 1015,
"slug": "any slug",
"title": "any title",
"description": "",
"parent": 0,
"post_count": 7
}],
"tags": [{
"id": 1180,
"slug": "any slug",
"title": "any title",
"description": "",
"post_count": 6
}, {
"id": 1179,
"slug": "religion",
"title": "religion",
"description": "",
"post_count": 5
}, {
"id": 1209,
"slug": "any slug",
"title": "any title",
"description": "",
"post_count": 4
}],
"author": {
"id": 1,
"slug": "newsdesk",
"name": "NewsDesk",
"first_name": "",
"last_name": "",
"nickname": "NewsDesk",
"url": "",
"description": ""
},
"comments": [],
"attachments": [{
"id": 44930,
"url": "anything appropriate",
"slug": "anything appropriate",
"title": "anything appropriate",
"description": "",
"caption": "",
"parent": 62296,
"mime_type": "image/jpeg",
"images": {
"full": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"thumbnail": {
"url": "anything appropriate",
"width": 150,
"height": 147
},
"medium": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"tie-small": {
"url": "anything appropriate",
"width": 55,
"height": 55
},
"tie-medium": {
"url": "anything appropriate",
"width": 272,
"height": 125
},
"tie-large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"big-slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
}
}
}],
"comment_count": 0,
"comment_status": "open",
"thumbnail": "anything",
"appropriate": {
"views": [
"2"
],
"taq_review_button_text": [
""
],
"taq_review_button_size": [
"medium"
],
"taq_review_button_shape": [
"square"
],
"taq_review_button_color": [
"#c7c7c7"
],
"taq_button_icon": [
"fa fa-check"
],
"taq_review_button_type": [
"flat"
],
"taq_review_button_url": [
""
],
"taq_review_title": [
""
],
"taq_review_position": [
""
],
"taq_review_style": [
"stars"
],
"taq_review_summary": [
""
],
"taq_review_total": [
""
],
"tie_hide_meta": [
""
],
"tie_hide_author": [
""
],
"tie_hide_share": [
""
],
"tie_hide_related": [
""
],
"tie_hide_check_also": [
""
],
"tie_sidebar_pos": [
"default"
],
"tie_sidebar_post": [
"category lifestyle"
],
"tie_post_head": [
"none"
],
"tie_post_slider": [
"59602"
],
"tie_googlemap_url": [
""
],
"tie_video_url": [
""
],
"tie_video_self": [
""
],
"tie_embed_code": [
""
],
"tie_audio_m4a": [
""
],
"tie_audio_mp3": [
""
],
"tie_audio_oga": [
""
],
"tie_audio_soundcloud": [
""
],
"tie_banner_above": [
""
],
"tie_banner_below": [
""
],
"tie_posts_num": [
""
],
"post_color": [
""
],
"post_background": [
"anything appropriate"
],
"tie_views": [
"2"
]
},
"thumbnail_size": "thumbnail",
"thumbnail_images": {
"full": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"thumbnail": {
"url": "anything appropriate",
"width": 150,
"height": 147
},
"medium": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"tie-small": {
"url": "anything appropriate",
"width": 55,
"height": 55
},
"tie-medium": {
"url": "anything appropriate",
"width": 272,
"height": 125
},
"tie-large": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
},
"big-slider": {
"url": "anything appropriate",
"width": 290,
"height": 147
}
}
}]
}


var newPost = {};
newPost.id = 65869;
newPost.categories = ["a", "b", "c", "d"];
newPost.author = {
"name": "Jimbo",
"last-name": "BobCat"
};

news.posts.push(newPost); //append the new post object.

document.write("<pre>" + JSON.stringify(news) + "</pre>")

为了表达我的意思。我创建了一个新的帖子对象并使用 news.posts.push(newPost) 将其附加。 push 用于将数据附加到数组。

关于javascript - 如何在 Javascript 对象数组的最后一个对象之后附加几个新对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28730516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com