如何在Javascript中对象数组的最后一个对象之后附加几个新对象?
我正在做一个移动应用程序(使用离子框架)的新闻网站是由wordpress。 json文件是从站点获取的。如果用户继续滚动,我必须从wordpress站点获取相同的重复json数据和一些新帖子,以便我的应用程序显示新消息。因此,我必须将这些新的几个posts对象添加/附加到以前的“posts”对象数组中。下面是传统的wordpress posts json文件对象的外观如何在Javascript中对象数组的最后一个对象之后附加几个新对象?,javascript,arrays,json,javascript-objects,Javascript,Arrays,Json,Javascript Objects,我正在做一个移动应用程序(使用离子框架)的新闻网站是由wordpress。 json文件是从站点获取的。如果用户继续滚动,我必须从wordpress站点获取相同的重复json数据和一些新帖子,以便我的应用程序显示新消息。因此,我必须将这些新的几个posts对象添加/附加到以前的“posts”对象数组中。下面是传统的wordpress posts json文件对象的外观 { "status": "ok", "count": 2, "count_total": 4231,
{
"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”数组中让我感到非常痛苦
我可以将“posts”数组保存到我的应用程序的变量中。但不能在最后一个posts对象之后将新获取的对象追加到“posts”数组中。使用push方法
// create the new object
var new_obj = {"key":"value", "next_key":"next_value"}
// append the new object to the array
posts.push(new_obj);
// display the values in the array
for (var i = 0; i < posts.length; i++) {
console.log(posts[i]);
}
要从json数据获取posts数组,请使用:
posts_array = json_obj.posts;
var新闻={
“状态”:“确定”,
“计数”:2,
“总数”:4231,
“页数”:2116页,
“员额”:[{
“id”:62296,
“类型”:“职位”,
“鼻涕虫”:“任何鼻涕虫”,
“url”:“任何url”,
“状态”:“发布”,
“头衔”:“任何头衔”,
“头衔”:“任何头衔”,
“内容”:“任何内容”,
“摘录”:“任何摘录”,
“日期”:“2015-02-26 02:08:57”,
“修改”:“2015-02-26 02:09:59”,
“类别”:[{
“id”:1015,
“鼻涕虫”:“任何鼻涕虫”,
“头衔”:“任何头衔”,
“说明”:“,
“父项”:0,
“后计数”:7
}],
“标签”:[{
“id”:1180,
“鼻涕虫”:“任何鼻涕虫”,
“头衔”:“任何头衔”,
“说明”:“,
“后计数”:6
}, {
“id”:1179,
“鼻涕虫”:“宗教”,
“头衔”:“宗教”,
“说明”:“,
“后计数”:5
}, {
“id”:1209,
“鼻涕虫”:“任何鼻涕虫”,
“头衔”:“任何头衔”,
“说明”:“,
“后计数”:4
}],
“作者”:{
“id”:1,
“slug”:“新闻台”,
“名称”:“新闻台”,
“名字”:“,
“姓氏”:“,
“昵称”:“新闻台”,
“url”:“,
“说明”:”
},
“评论”:[…],
“附件”:[{
“id”:44930,
“url”:“任何合适的”,
“slug”:“任何合适的”,
“头衔”:“任何合适的东西”,
“说明”:“,
“标题”:“,
“家长”:62296,
“mime_类型”:“图像/jpeg”,
“图像”:{
“完整”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“缩略图”:{
“url”:“任何合适的”,
“宽度”:150,
“高度”:147
},
“中等”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“大”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“领带小”:{
“url”:“任何合适的”,
“宽度”:55,
“高度”:55
},
“中号领带”:{
“url”:“任何合适的”,
“宽度”:272,
“高度”:125
},
“大领带”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“滑块”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“大滑块”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
}
}
}],
“注释计数”:0,
“评论状态”:“打开”,
“缩略图”:“任何东西”,
“适当”:{
“意见”:[
"2"
],
“taq_查看_按钮_文本”:[
""
],
“taq_审查_按钮尺寸”:[
“中等”
],
“taq_回顾_按钮_形状”:[
“正方形”
],
“taq_查看_按钮_颜色”:[
“#c7c7c7”
],
“taq_按钮_图标”:[
“不动产检查”
],
“taq_审查_按钮类型”:[
“平坦”
],
“taq_审查_按钮_url”:[
""
],
“taq_审查_标题”:[
""
],
“taq_审查_立场”:[
""
],
“taq_回顾风格”:[
“明星”
],
“taq_审查总结”:[
""
],
“taq_审查_总计”:[
""
],
“tie_hide_meta”:[
""
],
“tie_hide_作者”:[
""
],
“tie_hide_share”:[
""
],
“领带隐藏相关”:[
""
],
“领带隐藏检查”:[
""
],
“tie_侧边栏位置”:[
“默认值”
],
“tie_侧边栏_post”:[
“类别生活方式”
],
“领带杆头”:[
“没有”
],
“拉杆柱滑块”:[
"59602"
],
“tie_googlemap_url”:[
""
],
“tie_视频_url”:[
""
],
“tie_video_self”:[
""
],
“tie_嵌入代码”:[
""
],
“tie_audio_m4a”:[
""
],
“tie_audio_mp3”:[
""
],
“tie_audio_oga”:[
""
],
“tie_audio_soundcloud”:[
""
],
“上面的铁旗”:[
""
],
“下面的铁旗”:[
""
],
“tie_posts_num”:[
""
],
“post_颜色”:[
""
],
“后大学背景”:[
“任何合适的”
],
“tie_视图”:[
"2"
]
},
“缩略图大小”:“缩略图”,
“缩略图图像”:{
“完整”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“缩略图”:{
“url”:“任何合适的”,
“宽度”:150,
“高度”:147
},
“中等”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“大”:{
“url”:“任何合适的”,
“宽度”:290,
“高度”:147
},
“领带小”:{
“url”:“任何合适的”,
posts_array = json_obj.posts;
$scope.newsList = {
"status": "ok",
"count": 2,
"count_total": 4231,
"pages": 2116,
"posts": [
{
"id": 62296
},
{
"id": 62297
},
{
"id": 62296
},
{
"id": 62297
}
]
};
$scope.newNews= {
"status": "ok",
"count": 2,
"count_total": 4231,
"pages": 2116,
"posts": [
{
"id": 62296
},
{
"id": 62297
},
{
"id": 62296
}
]
};
if( $scope.newNews.status == 'ok' && $scope.newNews.count > 0 ){
var posts = $scope.newNews.posts;
for(var key in posts) {
if(posts.hasOwnProperty(key)){
$scope.newsList.posts.push(posts[key]);
}
}
}