From the article JavaScript 2D Array – Two Dimensional Arrays in JS, I see one way to store data is to put all properties of each element into one array:
let dataRepresentation1 = [
['John Doe', 20, 60, 'A'],
['Jane Doe', 10, 52, 'B'],
['Petr Chess', 5, 24, 'F'],
['Ling Jess', 28, 43, 'A'],
['Ben Liard', 16, 51, 'B']
];
I wonder how that compares to array of objects:
dataRepresentation2 = [
{
"id": "id",
"key2": "value2",
"key3": "value3"
},
{
"id": "id",
"key2": "value2",
"key3": "value3"
}
]
and having each property in their own array:
dataRepresentation3 = [
[ 'John Doe', 'Jane Doe', 'Petr Chess', 'Ling Jess', 'Ben Liard' ],
[ 20 , 10 , 5 , 28 , 16 ],
[ 60 , 52 , 24 , 43 , 51 ],
[ 'A' , 'B' , 'F' , 'A' , B ]
]
Here are my try to compare them:
Representation | 1 | 2 | 3 |
---|---|---|---|
Easy to comprehend | OK | Good | OK |
Easy to compute (modify, slice, filter, etc) | Good | ? | Bad |
Easy to get a specific value | Good | Good | OK |
Are these evaluations correct? Are there other criteria to include in? I'm mostly use JS and Python, if that matters.
See also: Array of objects vs Object of Objects
data.map(row=>({name: row[0], key2: row[1], ...etc }))
. columns to rows would be use a transpose function, etc