I am currently working on a task to eliminate all square brackets from the keys in the entries
field within an array of objects:
data: [
{title: "Title1", entries: { 'Entry1': 333333, '[ABC]Entry2': 1234, 'Entry3': 5555 }},
{title: "Title2", entries: { '[ABC]Entry1': 5555, 'Entry2': 4444,'Entry3': 2222 }}
]
The objective is to transform [ABC]Entry2
and [ABC]Entry1
into ABCEntry2
and ABCEntry1
, respectively.
To achieve this, I have iterated through the data
array and then looped over the keys using Object.keys(x.entries)
. Within that loop, I check for any existing brackets in the key, and if found, replace them with an empty string.
Below is the code snippet depicting this logic:
data.map(x => {
Object.keys(x.entries).map( y => {
if(y.includes('[') && y.includes(']')) {
y = y.replace(/\[/g, '').replace(/]/g, '');
console.log("The updated key without brackets: ", y);
}
})
console.log("Entries with brackets still present: ", x.entries);
})
console.log("Original data still containing brackets: ", data);
Although the console displays the desired result without square brackets when printing the y
value inside the loop, it reverts back to the original data structure outside the loop. Is there a way to directly update x.entries
and reflect these changes in data
without creating a new variable to store the modifications?