I have the following array and a picture of how the structure of it returns to me:
const connections = [1:{fromBox: 0, fromConnector: "port_0", toBox: 1, toConnector: "port_0"},
1:{fromBox: 0, fromConnector: "port_1", toBox: 1, toConnector: "port_1"}
]
and I need to create an object with this structure to be compatible with my code:
"connections": {
"0": {
"fromBox": "0",
"fromConnector": "port_0",
"toBox": "1",
"toConnector": "port_0",
},
"1": {
"fromBox": "0",
"fromConnector": "port_1",
"toBox": "1",
"toConnector": "port_1"
}
}
Looking at similar doubts I have understood that with a reduction method I should be able to achieve it but when trying to put the index, that they have in the array, into the object it gives me as if the elements of the array were undefined
const connectionsKeys = Object.keys(connections);
const result = connections.reduce((c, v) => {
c[v.connectionsKeys] = c[v.connectionsKeys] || {};
return c;
}, {});
Any help or guidance would be appreciated. Thank you in advance
connectionsis not a valid array.