0

I am trying to create a Cache Hashtable in Javascript.

by executing cache.splice(0,0, ...dataPage); I insert my data form the first till the dataPage.length position.

Let's say my dataPage size is always 10

My cache array should look like [0: {..}, 1: {..} ..... 9: {...}]

After that let's say I want to load the 5th page of data. I'm executing cache.splice(40,0, ...data);

I'm expecting my array to look like [[0: {..}, 1: {..} ..... 9: {...}, 40: {...}, 41:{...} ... 49{...}]

But it looks like [[0: {..}, 1: {..} ..... 9: {...}, 10: {...}, 11:{...} ... 12{...}]

Any idea how I can achieve my expected result??

1 Answer 1

1

cache.splice(40,0, ...data) means, that you ...data will be inserted in at the 40th position of your cache.

If your cache doesn't have 40 elements, it cannot be inserted at that position.

Maybe you should first define a cache with the desired amount of positions and then splice your data into it.

EDIT sure you can do it dynamically. Something like this (I didn't test it, but maybe you get the point):

var cache= [];
var insertion_pos = 40;
var dataPage = [0: {..}, 1: {..} ..... 9: {...}, 10: {...}, 11:{...} ... 12:{...}]

for(var i=0; i < insertion_pos; i++) {
   cache.push(i);
}

cache.splice(insertion_pos, 0, ...dataPage)
Sign up to request clarification or add additional context in comments.

1 Comment

So isn't there a way to expand my array dynamically in every insertion if needed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.