¿Cómo puedo eliminar algún carácter específico dentro de una matriz? por ejemplo;
var wording = ["She", "gives","me", "called", "friend"]; var suffix = ["s", "ed", "ing"]; function p { return wording.substring(wording.substring(wording) - 1, wording.length - 1)) } var text = wording.map(p); console.log(text);Si una de las palabras que se endsWith en una de las cadenas, puede cortar la longitud del sufijo encontrado.
var wording = ["She", "gives","me", "called", "friend"]; var suffix = ["s", "ed", "ing"]; const p = word => { const foundSuffix = suffix.find(str => word.endsWith(str)); return !foundSuffix ? word : word.slice(0, -foundSuffix.length); } var text = wording.map(p); console.log(text);Otro enfoque, usando una expresión regular:
const wording = ["She", "gives","me", "called", "friend"]; const suffix = ["s", "ed", "ing"]; const pattern = new RegExp(suffix.join('|') +'$'); const p = word => word.replace(pattern, ''); console.log(wording.map(p));MÉTODO 1
var wording = ["She", "gives","me", "called", "friend"]; var suffix = ["s", "ed", "ing"]; function p(w) { var ret = w; suffix.forEach(s => { if( w.endsWith(s) ) { ret = ret.slice(0,w.length - s.length); } }) return ret } var text = wording.map(p); console.log(text);MÉTODO 2
var wording = ["She", "gives","me", "called", "friend"]; var suffix = ["s", "ed", "ing"]; var text = wording //check if any of the words end with any of the suffices min 0, max 1 .map(w => [w,suffix.filter(s => w.endsWith(s))]) //use the returned array to remove suffix, if found .map(([w,s]) => s.length ? w.slice(0,w.length - s[0].length) : w); console.log(text);