0

I'm trying to create (or simulate) an infinite-dimensional array in JavaScript. Essentially, this would be an data structure that would associate objects with lists of integers (which could be of any length). Is there an efficient way to store each element in this data structure?

function addElement(theObject, coordinates){
    //object is the object, and coordinates is the list of coordinates (any number of coordinates will be accepted, since it's infinite-dimensional)
}

function getObject(coordinates){
    //get the object that was previously assigned to this list of coordinates
}
addElement("Hello World", [0, 0, 3, 5]);
console.log(getObject([0, 0, 3, 5])); //this would print "Hello World".
13
  • Why would you want to do this? Commented Jan 1, 2013 at 1:11
  • what do you mean infinite-dimentional? javascript arrays don't have a limit to it's length so it's already "infinite" if that's what you mean. Commented Jan 1, 2013 at 1:11
  • @WaleedKhan I'm trying to implement a quadtree in JavaScript, where elements can be added or removed from the quadtree by listing the coordinates, along with the object to add to the quadtree. Commented Jan 1, 2013 at 1:11
  • @kennypu Yeah, but arrays are one-dimensional. Commented Jan 1, 2013 at 1:11
  • 1
    @WaleedKhan yes, but a multi-dimentional array is merely an array within an array. There is no restriction on the depth of the array. Commented Jan 1, 2013 at 1:12

2 Answers 2

2

Unless there is any reason you can't, I would just use the coordinate as an index, and store things there:

var coordinates = [];
var testCoord = [0,0,3,5];
coordinates[testCoord] = "Hello World";
console.log(coordinates[testCoord]);
Sign up to request clarification or add additional context in comments.

6 Comments

You Should make it explicit that you are converting the coordinates [0, 0, 3, 5] to the string '0,0,3,5'.
Also, you should probably use coordinates = {} instead of [] because it's not really an array...
This is a simple and concise solution, but I'd like to know how it compares to the other solution (in terms of memory usage).
This is going to have the lowest memory usage of all: a single-level object, with one member per stored "coordinate".
@kennypu It's not neater because code should be written for humans, not for computers. Having secret conversions going on is, in my opinion, not best practice and can lead to bugs and misunderstanding by other people who will (one day, if your code has any quality and your company lasts) be maintained by someone else.
|
1

Absolutely. Just loop:

(function() {
  var store = [];
  window.addElement = function(theObject,coordinates) {
    var t = store, l = coordinates.length, i;
    for(i=0; i<l-1; i++) {
      if( typeof t[coordinates[i]] !== "undefined" && !(t[coordinates[i]] instanceof Array))
        (function(old) {(t[coordinates[i]] = []).toString = function() {return old;};})(t[coordinates[i]]);
      t = t[coordinates[i]] = t[coordinates[i]] || [];
    }
    t[coordinates[i]] = theObject;
  }
  window.getObject = function(coordinates) {
    var t = store, l = coordinates.length, i;
    for(i=0; i<l-1; i++) {
      t = t[coordinates[i]];
      if( !(t instanceof Array)) throw new Error("Invalid coordinate");
    }
    return t[coordinates[i]];
  }
})();

addElement("Hello World",[0,0,3,5]);
console.log(getObject([0,0,3,5]));

6 Comments

But... why not return the getObject and addElement in an object from the IIFE?
@WaleedKhan I'm not sure what you mean. The functions are being added to the window object.
Why add them to window? Why not add them to an object and then return that object?
@WaleedKhan Because the original question had them defined as functions in the global scope, so I kept to the same pattern. The code could easily be adapted into a class-like structure, but that wasn't how the question was asked.
@AndersonGreen Added code to fix that. Now if an element is already there, it is replaced with an array whose string value is set to be the existing value.
|

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.