I have an array of arrays in a variable using in JavaScript. I would like to update the value with the sub-array without having to copy the array temporary.
Here is an example.
I have this array
var data= [{
'123': [{'a': 10, 'b': 20, 'c': 30, 'd': 40}],
'456': [{'a': 1, 'b': 2, 'c': 3, 'd': 4}]
}];
I want to be able to change the value of a inside of the 123 key if a existing. Otherwise, I want to create that key. Also, if 'a' does not exists in the sub-array, I want to add it instead of throwing an error.
I am trying to update
data['123']['assigned_at'] = 'value';
but since assigned_at does not exist I am getting this error
Unable to set property 'assigned_at' of
undefinedornullreference.
How can I correctly update the array correctly?
var data = [{
'123': [{
'a': 10,
'b': 20,
'c': 30,
'd': 40
}],
'456': [{
'a': 1,
'b': 2,
'c': 3,
'd': 4
}]
}];
data['123']['assigned_at'] = 'value';
datais an array containing a single object which in return holds the keys123,456. Is that a typo? Oh and123and456are again arrays with only a single object - so the full path would be:data[0][123][0]["a"]