Receiving a JSON from an external source with an unpredictable number of keys is the challenge. The structure typically appears as follows:
data = [{
id: 1,
testObject_1_color: "red",
testObject_1_shape: "triangle",
testObject_2_color: "blue",
testObject_2_shape: "line",
},{
id: 2,
testObject_1_color: "green"
testObject_1_shape: "triangle",
},{
id: 3,
testObject_1_color: "brown",
testObject_1_shape: "square",
testObject_2_color: "black",
testObject_2_shape: "circle",
testObject_3_color: "orange"
testObject_3_shape: "square",
}]
To effectively process this data, transforming it into a more manageable format would be ideal:
data = [
{object:1, color:"red", shape:"triangle"},
{object:2, color:"blue", shape:"line"},
{object:3, color:"green", shape:"triangle"}
]
The fluctuating number of testObject_x_color / shape
properties makes iterating through the collection daunting without resorting to repetitive checks like
if data.hasOwnProperty('testObject_x_color')...
. Any suggestions on how to navigate this gracefully?