1

I'm storing some collision squares with properties in my JavaScript code. The easiest way seems to be an object using arrays as the index. My code should look something like this:

// left, top, width, height
const array = [0, 0, 8, 16];
var rectangles = {};
rectangles[array] = {foo: bar};

Wanted to make sure this is a valid and sound method. I remember people saying objects in JS are always indexed by string, this might not convert accordingly then. It thus seems safer to use a string (const s = array[0] + " " + array[1] + " " + array[2] + " " + array[3]) but I was hoping that doing it this way might allow me to extract entries by specifying their array so I wouldn't need my own string separator function to match the values.

// Obviously I'd use a more complex function to get the values I need
const this_rectangle = rectangles[[0, 0, 8, 16]];
5
  • 1
    is each array that you want to use as a key unique? Commented Nov 4, 2021 at 9:45
  • I'd say you're better of by fully acknowledging that your index must be a string, and as such include some helper function that derives that string from a given array. Whether that function actually simply concatenates the array values, or does something more elaborate is then changeable whenever you need it to. Commented Nov 4, 2021 at 9:47
  • 2
    rectangles[array] will convert array to a string. So the key for your object will be '0,0,8,16'. Commented Nov 4, 2021 at 9:47
  • Sounds like I should go with a string then, perhaps use rectangle.toString() when setting the object index to convert the array internally for safe measure. Thanks. Commented Nov 4, 2021 at 9:58
  • 1
    Can you clear up, whether the array containing the coordinates itself (it's object identity) or it's values provide identity? That's quite important with regards to what solutions are feasible (Map, WeakMap, ...). Commented Nov 4, 2021 at 10:11

2 Answers 2

1

How about you use a WeakMap :D

// left, top, width, height
const array = [0, 0, 8, 16];
var rectangles=new WeakMap(); //declaration
rectangles.set(array,{foo:"bar"}); //setting

//example
console.log( rectangles.get(array) ); //getting

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

4 Comments

Good idea, will keep in mind thanks!
This relies on the object identity of the array, which might be a problem.
indeed it does but they themselves talked about using arrays themselves as the index and not their values
You're right, it's still a bit unclear whether the array itself provides identity or it's values. Maybe they'll clear that up. ;)
0

Objects are indeed indexed by string exclusively. Found a convenient solution with this in mind:

const array = [0, 0, 8, 16].toString();
rectangles[array] = {foo: "bar"};

And to extract the positions back into an array when iterating through rectangles:

for(let pos in rectangle) {
    const data = rectangle[pos];
    const poses = pos.split(",");
}

1 Comment

what happens if 2 arrays have the same string 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.