I have to convert that handwrited array, to natural array.
var json = "[";
if(result.length > 0) {
json += `{ "${result[0].ped_id}": { "id": ${result[0].id}, "ocupped": ${result[0].ocupped} } }`;
}
json += "]";
The array should looking like that: [ { '1': { id: 1, ocupped: 0 } } ], someone have an idea what i can do?
Actually, you should create the object first, then JSON.stringify it.
let result = [{
ped_id: 101,
occuped: "something"
}]
let json = [];
if (result.length > 0) {
json.push({
[result[0].ped_id]: {
id: result[0].ped_id.toString(),
ocupped: result[0].ocupped
}
})
}
json = JSON.stringify(json)
console.log(json)