Javascript: Replacing items in array with another set of items -
i have app fetch data server , keep in array. when display want display paginated decided have 50 items per page.
i want implement refresh functionality can refresh current viewed page. example, if i've loaded 3 pages already, means have 150 items in array, , refresh page 2 (which means fetch items 50-99 server) want replace current items new items.
basically want remove items 50-99 in current array , insert "new" 50 items current array starting @ index 50.
i tried doing with:
// remove items items.splice((this.currentpage*50 -50), 50) // add new freshly grabbed items instead array.prototype.splice.apply( items, [(this.currentpage*50 -1), data.data.length].concat(data.data));
but seem work kinda buggy. if have 1 page loaded works expected. deletes of 50 items , replaces them new items. however, if have 2 pages loaded , try refresh page 1 can see current items array length 99 instead of 100. tried playing can't work :/
any idea how can efficiently delete set of items array based on current refreshed page , replace deleted items new array has new items?
i think, not need this.
// remove items items.splice((this.currentpage*50 -50), 50)
otherwise need implement 0
length this:
// add new freshly grabbed items instead array.prototype.splice.apply( items, [(this.currentpage - 1) * 50, 0].concat(data.data)); // ^
the best thing, in 1 step this:
var = array.apply(null, { length: 150 }).map(function (_, i) { return i; }), b = array.apply(null, { length: 50 }).map(function (_, i) { return + 1050; }); array.prototype.splice.apply(a, [50, b.length].concat(b)); document.write('<pre>' + json.stringify(a, 0, 4) + '</pre>');
Comments
Post a Comment