简体   繁体   中英

Merging Objects in Javascript for Infinite Loading Ajax Plugin

I am trying to implement infinite scrolling. An ajax request returns an object that includes 10 threads. This ajax request is called every time the user scrolls to the bottom. What I am looking for is a way to merge these objects together.

One object looks like this:

Article: Object
    page: 1
    last_page: 20
    data: Array[10]
        0: Object
        1: Object
        2: Object

Inside of the data array objects are arrays and objects again... So my problem here really is that we're speaking of a quite large object containing a variety of objects and arrays. Any idea how I could manage to do this?

Thank you! <3

To add an array to another you could use the following code:

// `b` onto `a`:
a.push.apply( a, b );

More information on the best way of merging/combining arrays may be found at this link .

In your case it would seem you keep one big pile of data (in the end). That would mean you would push the new article data on top of the pile. Since you know that this pile will be the biggest, it would give a good performance if you used this:

var articlePile = article.data;
// For every new article push the data onto the pile
articlePile.push.apply( articlePile, article2.data );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM