1

I'm trying to setup a multidimensional array where I can push value into it where it needs to.

For example, I have a cacheArray = new Array(col0Array, col1Array, col2Array); which would accessible using cacheArray[0] etc...

Now, in a loop I would check cacheArray[column_number].length, which would be the length of whichever colArray. Depending on that I want to cacheArray[column_number].push("my_class");

My intention is to add the css class name to the sub column arrays, and check the previous value in the sub array for that column so I can get a checkered colour effect. But pushing like I did gives me an error: cacheArray[column_number].push is not a function.

Any ideas on how to approach this problem?

3 Answers 3

1

It's not entirely clear how you initialize your data structure. This will initialize an array of three columns where each column is itself an array:

var cacheArray = [[], [], []];

You can then push values into each of those subarrays:

cacheArray[0].push("my_class");
cacheArray[0].push("his_class");
cacheArray[0].push("nobodys_class");
cacheArray[1].push("their_class");
cacheArray[2].push("your_class");

That would result in this data structure for cacheArray:

[["my_class", "his_class", "nobodys_class"], ["their_class"], ["your_class"]];

In a modern browser, you can see it work here: http://jsfiddle.net/jfriend00/f9hy4/. I say modern browser just because the sample code requires JSON.stringify() to visualize the results.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe I am missing something but if you are initialising a multi (as opposed to a two) dimensional array, then cacheArray[column_number].push would not exist as a function because you are only providing one of the dimensions.

cacheArray[column_number].push("my_class") would work if you initialised a two dimensional array because you would be pushing on to the array that is at cacheArray[column_number]. Thus if column_number was 1, and this was the first item you had pushed cacheArray[1][0] would equal "my_class".

I can't help thinking there is an easier way to do this anyway, perhaps an array of objects which could hold the last and current values for each column?

Comments

0

You have declare your array cacheArray as single dimensional make it as two dimensional array by initialize it.
you are actually pushing a new value to the cacheArray[column_number][some_index] that is doesn't exits, this statement indicate that you are pushing a my_class in the index cacheArray[column_number][some_index] that is not present, that's why it gives error.

you may try this:
cacheArray = new Array(col0Array, col1Array, col2Array);
for(var i = 0, i = cacheArray.length; i++)
cacheArray[i] = new Array();
cacheArray[column_number].push("my_class");

Comments

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.