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

93
Views
Insert iterated keys inside another object in Javascript

I have an object that receives coordinates values ​​like this

Object{
[key]:[value],
[key]:[value],
}

Each key is a coordinate x,y and each value is a true boolean. I generate the coordinates through a program that receives an array with values ​​0 for true and 1 for false, a width and a height.

I want to output each key as a coordinate x,y inside that object in this way:

Object{
[gridCoords([0, 0, 0, 0,
             1, 1, 0, 1,
             0, 0, 0, 0,
             0, 0, 0, 0],
             4,
             4)]:true,
}

So, my function is something like this:

function gridCoords(data, height, width)  {
  var obj = [];
  for (let row = 0; row < height; row++) {
    for (let col = 0; col < width; col++) {
      let key=`${row * 16},${col * 16}`
      let value= (data[row * height + col] === 1 ? true: false )
      if (value){
        obj.push(key)
      }
    }
  }
  return obj
}

But i received this:

{
16,0,16,16,16,48,32,16,32,32: true
}

Instead of:

{
16,0: true,
16,16: true,
16,48: true,
32,16: true,
32,32: true
}

What I can do?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think you want an object not an array here:

function gridCoords(data, height, width)  {
  var obj = {};
  for (let row = 0; row < height; row++) {
    for (let col = 0; col < width; col++) {
      let key=`${row * 16},${col * 16}`
      let value= (data[row * height + col] === 1 ? true: false )
      if (value){
        obj[key] = value
      }
    }
  }
  return Object.keys(obj)
}
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!