How to remove £ symbol from nested array

Hi

I’m trying to remove the £ symbol from array but not having much luck. This is my array:

var linedata = [["£14.99,£14.99,£14.99"],["£34.99,£34.99,£34.99"]]

and this is the regex code I’m using:

const regex = /£([0-9.]+,?)/gm;
const subst = `$1`;
const result = linedata.replace(regex, subst);
console.log('substituted', result)

But its not working and I don’t know why, is it an ionic specific issue? The Ionic CLI is spitting out this error, but I don’t really know what it means, is it to do with me trying to use replace on an array?..

ERROR in src/app/home/home.page.ts(66,26): error TS2339: Property 'replace' does not exist on type 'any[]'.

Any help appreciated!

Just like what the error specifies, your linedata is an array of string arrays (or, array of arrays)
.replace is a method of String type values

What you should do here is to iterate through each item inside the array and do the replace to each string item

As already mentioned, you can’t apply replace on an array. replace happens on a string. Also, using regex for something so simple is an overkill. You could just use substring() to get string after the first character effectively removing the first character.

var linedata = [["£14.99,£14.99,£14.99"],["£34.99,£34.99,£34.99"]];

console.log('before => ',linedata);

for(var i=0;i<linedata.length;++i){
 for(var j=0;j<linedata[i].length;++j){
    linedata[i][j] = linedata[i][j].split(",").map(value => value.substring(1)).join(",");
 }  
}

console.log('after => ',linedata);

Thank you @jusliong that makes sense now!

This is brilliant thank you @vivek-23

1 Like

Great. If this solves your problem, can you mark it as accepted?