Reputation: 63
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?
Upvotes: 0
Views: 35
Reputation: 1178
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)
}
Upvotes: 2