0

There are fixed three columns in it. The rows are added dynamically and may go up-to few thousands also.

I should also be able to iterate through the array and filter Id & Values based on level.

Is this possible ? How?

enter image description here

milan m
  • 2,164
  • 3
  • 26
  • 40
  • http://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript – jbutler483 Aug 22 '14 at 08:26
  • 1
    Yes, it is certainly possible. – tucuxi Aug 22 '14 at 08:27
  • You could build it from the ground up. But you could start with [DataTables](http://www.datatables.net/examples/index) – sri Aug 22 '14 at 08:29
  • @hindmost, of course I have tried. I didn't write the code in my question because I don't want my code to influence other answers. – milan m Aug 22 '14 at 09:33

1 Answers1

1

Arrays of Arrays are easy.

[[1, 234,'Apple'],[2,23,'Sunday'], ....]

To add something to the array, push use push. To iterate forEach. filter is not yet in the JS standard, but here is a polyfill: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

I suggest for your problem to look at not building an Array of Arrays, but model each row as a JS object:

{level: 1, id: 234, value: 'Apple'}

that way you can write more semantic code like

myObjectList.filter(function(obj){ return obj.level > 1 })

rather than using array indexes everywhere.

In general if you ant get an idea what you can do with JS built-ins like Array, check the JS reference at the Mozilla Developer Network. Its pretty good and has lots of examples for each Array function like forEach or find, or filter.

semiomant
  • 584
  • 6
  • 12