Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'd like to add a bulk of items into a sparse array, starting at a certain index. I came along this requirement in order to accomplish client side pagination while not retrieving all data from server (assuming server returns the right data, given the required indexes).

To keep right indexes when "Skipping" and "Taking", I need to store the items in a sparse array. Besides relevant indexes, other places in array might as well be undefined.

I wrote the following snippet of code, assuming bulk is the retrieved chunk of items from Server, and index is where the first item should be placed.

    items = new Array(totalItemsPossibleCount);
    items = items.slice(-index)
                 .concat(bulk)
                 .concat(items.slice(index+bulk.length));
  1. Is there a shorter way to do so?
  2. Can this be achieved with underscore.js in more readable way?
share|improve this question
1  
Your problem starts with "I need to store the items in a sparse array". You really really don't need to store the items in a sparse array here. Sparse arrays are not just slow, they're also not really conceptually arrays. You can use an object as a map for example, or better yet have objects for pages and a non sparse array for those. Remember, you also have a bunch of other collections available at your disposal, a deque might be a good choice here too if users usually navigate to adjacent pages. –  Benjamin Gruenbaum Apr 6 at 17:37

2 Answers 2

You don't need to worry about sparse arrays in JavaScript and there is no need to instantiate with the Array constructor. If you insert into a random index then the rest of the array is just padded with undefined references:

var items = [];
items[5] = 'foo';
console.log(items); // [5: "foo"]
items.toString();   // ",,,,,foo"

To add new items into the middle of an array you can use the splice function:

items.splice(index, 0, bulk);

The index is where to start adding items, 0 is how many items to remove after that point, and bulk is the new items to add at that point. Splice acts on the array directly, and returns any items that were removed.

share|improve this answer
    
That will work for a single item. I want to add a bulk which is array of items. I was using Array C'TOR to boost performance. I saw here you can use splice in the manner I'd love to. The latter actually answers my question. –  user40171 Apr 6 at 15:40
    
@user40171 Sorry, yes you can use apply with the arguments in order to add an array. –  Jivings Apr 6 at 16:08

As Jlivings points out, splice is what you're looking for. To use it for multiple items you could conceivably use apply:

var args = [index, 0].concat(bulk);
items.splice.apply(items, args);

Edit: Ah, shucks. Didn't see your comment on jlivings' answer; you obviously already found the same code solution over on SO.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.