Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
JavaScript: Index object by array?

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]];
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

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(",");
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!