I have the following JS code:
var expected_dict = {}
for (var i = 1; i < expected_data.length; i++) {
if (expected_data[expected_data[i][0]] == undefined) {
expected_dict[expected_data[i][0]] = {}
}
expected_dict[expected_data[i][0]][expected_data[i][20]] = {}
expected_dict[expected_data[i][0]][expected_data[i][20]] = expected_data[i][21]
console.log(expected_dict)
}
expected_data is an array of arrays, like
[
['revisionid1', ..., 'code1', 'cp1']
['revisionid1', ..., 'code2', 'cp2']
]
I’d expect the code to add values to the expected_dict revisionid key, but the output ends up being
{ 'revisionid1': { 'code1': 'cp1' } }
{ 'revisionid1': { 'code2': 'cp2' } }
Which means it looks like it’s overwriting the dict each time.
What I want would be
{ 'revisionid1': { 'code1': 'cp1', 'code2': 'cp2'} }
Is there something I’m not understanding about how JS handles dicts?
2
Answers
Try this
Reduce the data to a dict, with the first element as a dict key, the remaining items act as key/value pairs for a dict element: