I know that javascript objects have no inherent order for the key/value pairs it contains, but I have run into an interesting problem, and hopefully, someone can point me to a solution.
I'm developing an angular app that needs to iterate through an array of objects and display the value for a given key in the UI. This is tabular data. Pretty basic. The problem comes from the backend data sources. The data is aggregated from multiple sources, and not all objects get all key/values. We have no control over the backend. Furthermore, there are a variable number of key/value pairs. So, what I get on the frontend may look look like this:
[{ A: a, B: b, C: c}, {B: b, C: c}]
I can iterate the returned array and add missing keys, but things may be out of order, i.e.:
[{ A: a, B: b, C:c}, {B: b, C: c, A: a}]
which, course leads to an incorrect display of data.
for(var val of dataArray){
for(var header of headers){
val.environmentMetrics[header] = val.environmentMetrics[header] || 0;
}
}
headers is an array of strings that match the keys. The first object in the returned data contains the keys, which are used for the table headers.
So whats the best way to get my data order consistently? Thanks