I have like this strings in array.
const x = [
'a',
': 99999999999999999999999999,',
':',
'this, is 2 :)'
]
How can I remove : and , from second element like this?
const x = [
'a',
'99999999999999999999999999',
':',
'this, is 2 :)'
]
I tried replace : and , to ''. But there are these characters in other elements. And I don’t want to remove it. Also I tried startWith(‘:’), but it becomes all [undefined,undefined,undefined,undefined].
const v = u.map((x) => {
x.startsWith(':') ? x.replace(':', '') : x;
});
console.log('v', v);
>Solution :
You are getting undefined values because you are not returning anything inside your .map() (common mistake, been there, done that). This would fix it:
const v = u.map((x) => {
return x.startsWith(':') ? x.replace(':', '') : x;
});
// Or even shorter
const v = u.map((x) => (x.startsWith(':') ? x.replace(':', '') : x);