Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript list like data structure?

It seems, if I'm not wrong, that because of the way Javascript handles Objects it is unpractical or inefficient to implement linked lists.

I would need a data structure in which I could easily do 2 operations(apart from indexing), appending at the end and removing (popping) an object at a given index.

Is using an Array and "recreating" it for each remove operation the optimal solution? I would think not.

Any ideas?

like image 254
Manux Avatar asked Aug 03 '10 17:08

Manux


3 Answers

It sounds like the JS Array is exactly what you're looking for.
You should be able to use the push and pop functions for the stack-like data structure and splice for the rest of it.

like image 158
Justin Avatar answered Sep 19 '22 17:09

Justin


Actually Array supports push and pop operations: JavaScript Array Object

like image 43
DixonD Avatar answered Sep 20 '22 17:09

DixonD


You don't have to recreate the Javascript array for each removal. Javascript Arrays have push() and pop() methods to add and remove elements:

JavaScript Array Object

like image 34
Justin Niessner Avatar answered Sep 21 '22 17:09

Justin Niessner