In my array of objects, I have the following data:
arrayOfObject = [{'key1': [1,2]} , {'key2': [1,2,3]} , {'key3': [1,2,4]}]
I know the name of the key that I want to replace in my array :
var keyString = 'key1';
And I also have the new name that this key should be changed to:
var newKey = 'newKey'
My goal is to transform my array into this format:
arrayOfObject = [{'newKey': [1,2]} , {'key2': [1,2,3]} , {'key3': [1,2,4]}]
To achieve this, I found a helpful resource on JavaScript Object Key Renaming, but I am unsure how to access the object that contains the initial key 'key1'
in the array.
Please note that the keyString
value will be different each time. Therefore, simply using arrayOfObject[0]
won't solve the problem.
Here is the link to the working example on JSFiddle: https://jsfiddle.net/tcatsx6e/