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

221
Views
Replace words with array map

I have this array

(2) ['beginning=beginner', 'leaves=leave']

and this string

its beginner in the sounds leave

which i have converted to an array

var words = text.split(' ');

i want to replace beginner with beginning and leave with leaves it can be any dynamic words but for now it has only two elements i can replace it within for loop. Is it possible with map method.

this.words.map((words, i) => console.log(words));

Note: Only first instance should get replaced.

Any Solution Thanks

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

0

does this correct with your question ?

const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];

let string = "its beginner in the sounds leave";

arrString.forEach((mapString) => {
  const stringArr = mapString.split("=");
  string = string.replace(stringArr[1], stringArr[0]);
});

console.log("string", string);
// "its beginning in the sound leaves"
about 4 years ago · Juan Pablo Isaza Report

0

You can do it in without a nested loop too if you compromise with space.

Here is a solution which creates a mapping object for replacement values and uses array methods like map(),forEach(),join() and string method like split()

const arrString = ["beginning=beginner", "leaves=leave", "sound=sounds"];

let string1 = "its beginner in the sounds leave beginner";
const replaceObj = {};
const arrBreaks = arrString.forEach(x => {
let els = x.split("=");
replaceObj[els[1]] = els[0]; });

const ans = string1.split(' ').map((x) => {
  if(x in replaceObj) { let val = replaceObj[x]; delete val; return val; }
  return x;
}).join(' ');

console.log(ans);

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!