如何在 Javascript 中对象数组的最后一个对象之后追加几个新对象?

How to append several new objects, after the last object of an array of objects in Javascript?

我正在制作一个由 wordpress 组成的新闻网站的移动应用程序(使用 Ionic 框架)。 json 文件是从站点获取的。如果用户继续滚动,我必须从 wordpress 网站获取一些新帖子的相同重复 json 数据,以便我的应用程序显示新新闻。所以我必须将这几个新的帖子对象 add/append 放到以前的 "posts" 对象数组中。这是常规的 wordpress 帖子 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" 数组中让我感到非常痛苦。

我可以将 "posts" 数组保存到我的应用程序中的变量中。但不能在最后发布对象后将新获取的对象附加到 "posts" 数组中。

使用推送方式。

// 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 对象,请使用:

json_obj.posts.push( new_obj);

要从 json 数据中获取帖子数组,请使用:

posts_array = json_obj.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>")

表达我的意思。我创建了一个新的 post 对象并使用 news.posts.push(newPost) 附加了它。 push 用于向数组追加数据。

如果你仔细看操作,这很简单。你有一个对象,假设 $scope.newsList = {}

$scope.newsList = {
    "status": "ok",
    "count": 2,
    "count_total": 4231,
    "pages": 2116,
    "posts": [
        {
          "id": 62296
        },
        {
          "id": 62297
        },
        {
          "id": 62296            
        },
        {
          "id": 62297
        }
    ]
};

为了简单起见,我认为您的 post 对象仅包含每个 post 的 ID。现在您可能已经附加了一个事件处理程序来处理滚动事件并一直获取新的 posts。让我们假设您有另一个对象,就像这个对象一样,并将它们存储在另一个范围变量 $scope.newNews 中,并假设这就像前一个对象 $scope.newsList

$scope.newNews= {
    "status": "ok",
    "count": 2,
    "count_total": 4231,
    "pages": 2116,
    "posts": [
        {
          "id": 62296
        },
        {
          "id": 62297
        },
        {
          "id": 62296            
        }
    ]
};

您在 newslist 数组中获得了 4 个 post,在 newNews 数组中获得了 3 个新的 post。当您将新的附加到旧的时,您应该在旧的新闻数组中得到 7 posts。

  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]);        
      }
    }
  }

首先我们检查新到达的对象的状态是ok还是什么以及接收到的post个数是否为空。您的新闻对象包含我们感兴趣的 post 个对象的数组。我们可以通过 $scope.newNews.posts 访问该数组。这是 posts 数组,我们需要使用它。现在我们得到了我们的数组,我们将继续一个循环,它将迭代我们到这个包含所有 post 对象的数组的每个 post 元素。在那里我们将检查 属性 是否存在(通过使用 posts.hasOwnProperty(key))。如果数组中存在 属性 key 表示数组中存在 key 索引(也就是说 value 也存在于 key 中),我们将推送旧新闻数组中的值。我们可以通过 $scope.newsList .posts.push(posts[key]);

访问它

现在,如果您检查原始或旧 posts 数组中的 posts 列表,您应该得到所有 posts。

希望这对您有所帮助。