I have an array of objects, which may have certain characters that I wish to remove. In the example it is £. My array is structured as below:
const testArray = [
{ ASIN: 'ABC123', Rank: '£50', Sales: '£80' },
{ ASIN: 'ZYX123&', Rank: '£70', Sales: '£20' },
];
I wondered if I could use something like the replace function by taking the array, splitting it into a string, using replace and then joining it up again.
e.g. textArray.split(',').replace(/^\£/, '').join(',')
Someone showed me this code, but I couldn't get it to work:
myArray.map(item => {
return Object.entries(item).reduce((agg, [key, value]) => {
agg[key] = value.replace(/^\£/, '');
return agg;
}, {});
});
Would my splitting into a string and rejoin method work? Is there a best practice way of doing this?