Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

96
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda