Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅将数组的最后一个元素添加到现有数组中_Javascript_Arrays_Angular_Ionic Framework - Fatal编程技术网

Javascript 仅将数组的最后一个元素添加到现有数组中

Javascript 仅将数组的最后一个元素添加到现有数组中,javascript,arrays,angular,ionic-framework,Javascript,Arrays,Angular,Ionic Framework,我有一个现有的数组,当我滚动时,我试图添加更多的元素 我正在使用将rss提要转换为json ngOnInit() { this.getRssFeed(); // returns 4 items } 以下是我添加更多项目的方式: this.count++; this.podcastService.getRssFeed(this.rssUrl, this.count) .then(data => { if (data) { for

我有一个现有的数组,当我滚动时,我试图添加更多的元素

我正在使用将rss提要转换为json

 ngOnInit() {
    this.getRssFeed();  // returns 4 items
}
以下是我添加更多项目的方式:

this.count++;
this.podcastService.getRssFeed(this.rssUrl, this.count)
    .then(data => {
        if (data) {
            for (const episodes of data.items) {
                this.episodes.push(episodes);  // returns 5 items
                // const episode = this.episodes[episodes.length - 1]
            }
            event.target.complete();
            console.log(data);
            ...
计数正确地递增。但是每次调用
getRssFeed
时,都会返回整个数组。每次使用正确的长度。 我不知道如何
pop
所有返回的数组元素,最后一个除外

我也尝试过类似的方法,只返回最后一个数组元素。还是不走运

const episode = this.episodes[episodes.length - 1] 
例如,如果在初始负载时,我得到:

[foo, bar]
当我滚动时,我返回:

[foo, bar, baz]
我只想将
baz
添加到已经存在的数组中


谢谢你的建议

据我所知,您只想将从
getRssFeed
服务返回的最新项目添加到
剧集
列表中。您可以使用数组排列语法在每次调用
getRssFeed
服务时更新剧集列表

您可以更新该函数,使其如下所示:

this.count++; 
this.podcastService.getRssFeed(this.rssUrl, this.count)
    .then(data => {
        if (data) {
            this.episodes = [
               // This keeps the previous episodes
               ...this.episodes,
               // This adds the last item returned to the array
               data.items[data.items.length -1],
            ]
        }
        event.target.complete();
        console.log(data);
        ...

您可以尝试的一种解决方案是更改代码的下一部分:

if (data)
{
    for (const episodes of data.items)
    {
        this.episodes.push(episodes);  // returns 5 items
        // const episode = this.episodes[episodes.length - 1]
    }
...
}
通过这个:

if (data)
{
    let lastEpisode = data.items.pop();
    this.episodes.push(lastEpisode);
...
}
这里,
pop()
用于从
数据中删除最后一个元素。items
数组并返回该元素,我们将其保存在变量
lastSpidence
中,最后将其推送到
Spidents
数组中。另一种解决方案不会改变
数据。项
数组可以是:

if (data)
{
    let lastEpisode = data.items[data.items.length - 1];
    this.episodes.push(lastEpisode);
...
}

您是否尝试过
此.scents.push(集[scents.length-1])
?你的问题不清楚。是的,我很抱歉我更新了我的问题。我只是想了解,
数据。items
是一系列情节吗?在这种情况下,您可以尝试
if(data)this.scents.push(data.items.pop())@Shidersz你是对的-它成功了!我试图找出使用
splice
和其他东西的方法。请更新您的建议作为答案,我会标记它。如果你不介意,你能解释一下吗?非常感谢你的帮助@也修改data.items的Shidersz。我不明白为什么OP只想弹出最后一个项目…?每次创建一个新数组似乎效率低下,为什么不干脆
this.scents.push(data.items[data.items.length-1])
?我希望我能接受所有这些。非常感谢大家!!