Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda