I have a JavaScript object that I receive from an API that I cannot modify. I would like to use this data within a tool, but the tool only accepts values stored on the root level of the object e.g. I cannot use dot notation to access the value of a key a level down.
Based on the example below, I'd like to be able to access shape.colour
, but by referencing something like shape__colour
instead, from a new JSON object.
Example starting object:
[{
"id" : 12345,
"size" : 40,
"shape": {
"colour" : 'yellow',
"dimension" : '2D'
}
},
{
"id" : 12346,
"size" : 50,
"shape": {
"colour" : 'blue',
"dimension" : '3D'
}
}]
What I need it to look like:
[{
"id" : 12345,
"size" : 40,
"shape__colour": 'yellow',
"shape__dimension" : '2D;'
}
},
{
"id" : 12346,
"size" : 50,
"shape__colour": 'blue',
"shape__dimension" : '3D'
}
}]
Other examples of object flattening functions that I've come across seem to produce a single level array (removing the objects altogether), whereas I need to keep the individual objects, but for the data inside them to be on one level.
Any help would be greatly appreciated!