Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Usando The .replaceAll () en una matriz literal de cadena en Javascript

Objetivo: usar un método de matriz para eliminar las cadenas get , right , the , first , time y reemplazarlas con la cadena única know en la matriz secretMessage.

Plan Defina una función para enviar mensajes de texto bajo la lluvia, la cadena anterior y la cadena nueva y use los métodos .replaceall() y pase cada uno de los diferentes argumentos a través de uno a la vez, reasignando la matriz cada llamada.

Código hasta ahora

 let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript']; function replaceWords(orginalArray,oldString,updatedString){= let updatedArray= orginalArray.replaceAll(oldString,updatedString);//Not Sure If I need to wrap this in [] return updatedArray; } secretMessage = replaceWords(secretMessage,'get','know'); secretMessage = replaceWords(secretMessage,'right','know'); secretMessage = replaceWords(secretMessage,'the','know'); secretMessage = replaceWords(secretMessage,'first','know'); secretMessage = replaceWords(secretMessage,'time','know'); console.log(secretMessage);

Resultados actuales

 let updatedArray= orginalArray.replaceAll(oldString,updatedString); ^

Error de tipo :

originalArray.replaceAll no es una función en replaceWords

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Como se indica en el comentario de @Pointy, replaceAll() es un método de cadena, no está disponible en una matriz.


Así que necesitaremos una forma de aplicar algo en cada índice de matriz .


Afortunadamente map() hace exactamente eso:

El método map() crea una nueva matriz con los resultados de llamar a una función proporcionada en cada elemento de la matriz de llamada .

 let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript']; function replaceWords(orginalArray,oldString,updatedString){ return orginalArray.map(item => { return item.replaceAll(oldString, updatedString) }); } secretMessage = replaceWords(secretMessage,'get','know'); secretMessage = replaceWords(secretMessage,'right','know'); secretMessage = replaceWords(secretMessage,'the','know'); secretMessage = replaceWords(secretMessage,'first','know'); secretMessage = replaceWords(secretMessage,'time','know'); console.log(secretMessage);

about 4 years ago · Juan Pablo Isaza Report

0

replaceAll es una función de cadena , no una función de matriz . Si desea reemplazar una palabra que tal vez exista como valor en una matriz, debe recorrer esa matriz. Su código se vería así:

 let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript']; function replaceWords(orginalArray,oldString,updatedString){ /* because your array values are just single words and you want to replace the whole word with another whole word, you can use string comparison instead of replaceAll() */ let updatedArray= orginalArray.map(string => (string == oldString ? updatedString : string)); return updatedArray; } secretMessage = replaceWords(secretMessage,'get','know'); secretMessage = replaceWords(secretMessage,'right','know'); secretMessage = replaceWords(secretMessage,'the','know'); secretMessage = replaceWords(secretMessage,'first','know'); secretMessage = replaceWords(secretMessage,'time','know'); console.log(secretMessage);

about 4 years ago · Juan Pablo Isaza Report

0

Solo por "diversión", una solución que use un método de matriz ( Array.prototype.join() ) y replaceAll() ( String.prototype.replaceAll() ) y cercana a su idea original, podría ser:

 function replaceWords(arr, oldStr, newStr) { return arr.join(' ') // join all the array items into a single space-separated string .replaceAll(oldStr, newStr) // call `replaceAll()` on the newly created string .split(' '); // split the string back into an array } let secretMessage = ['Learning', 'is', 'not', 'about', 'what', 'you', 'get', 'easily', 'the', 'first', 'time,', 'it', 'is', 'about', 'what', 'you', 'can', 'figure', 'out.', '-2015,', 'Chris', 'Pine,', 'Learn', 'JavaScript']; secretMessage = replaceWords(secretMessage, 'get', 'know'); secretMessage = replaceWords(secretMessage, 'right', 'know'); secretMessage = replaceWords(secretMessage, 'the', 'know'); secretMessage = replaceWords(secretMessage, 'first', 'know'); secretMessage = replaceWords(secretMessage, 'time', 'know'); console.log(secretMessage);

Habiendo dicho eso, creo que nadie lo haría de esa manera en la vida real ;)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!