0

I'm working on a JS applet that requires replacement of array entries in a given string with entires from another array. This is my code as it now stands:

const string = "Lion, Unicorn, Unicorn";
const array1 = ["Lion", "Unicorn"]; 
const array2 = ["Fox", "Hound"];
const string2 = string.replaceAll(array1[0],array2[0]) //returns "Fox, Unicorn, Unicorn"

My desired output is : Fox, Hound, Hound. Specifically, I'd like to turn the contents of string2 into a function that repeats for every item in an array, but have no idea where to start.

Thanks!

asked Apr 13, 2021 at 19:38
1
  • Consider using map? Commented Apr 13, 2021 at 19:58

3 Answers 3

1

Do you mean something like this? I hope I understood the question well.

You can write a recursive function:

let string = "Lion, Unicorn, Unicorn";
let array1 = ["Lion", "Unicorn"]; 
let array2 = ["Fox", "Hound"];
function myCustomReplace(str, a1, a2) {
 let wordToReplace=a1.shift(); // a1[0] - if array change matters
 let replacementWord=a2.shift(); // a2[0] - if array change matters
 if (!wordToReplace || !replacementWord) return str;
 str=str.replaceAll(wordToReplace, replacementWord );
 return myCustomReplace(str,a1,a2); // rturn myCustomReplace(str,a1.slice(1),a2.slice(1)) - if array change matters
}
console.log(
 myCustomReplace(string,array1,array2)
)

answered Apr 13, 2021 at 19:59
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was exactly what I was looking for!
0

It's sometimes worthwhile to first transform the inputs into a shape that is easier to work on. For this problem, the input sentence is better thought of as an array of words, and the two arrays used for replacement are better represented as a single object mapping input words to output words...

let string = "Lion, Unicorn, Unicorn";
let array1 = ["Lion", "Unicorn"]; 
let array2 = ["Fox", "Hound"];
// transform the inputs
let input = string.split(", ");
let translator = array1.reduce((acc, key, i) => {
 acc[key] = array2[i];
 return acc;
}, {});
// now input is ['Lion', 'Unicorn', ...]
// and transator is { 'Lion' : 'Fox', ... }
// now the problem is just a two-liner, mapping the input over the translator
let output = input.map(e => translator[e] || e)
console.log(output.join(", "))

answered Apr 13, 2021 at 20:16

Comments

0

If we use split(', ') to convert the string to an array of single words, we can use map() to replace them by searching for a pair with indexOf():

Please see comments in the code. A one-liner can be found at the end.

const string = "Lion, Unicorn, Unicorn";
const array1 = ["Lion", "Unicorn"]; 
const array2 = ["Fox", "Hound"];
// Split on ', '
let splitted = string.split(', ');
// Map
let result = splitted.map(w => {
 // Get position in array1
 const i = array1.indexOf(w);
 
 // If we've found something
 if (i !== -1) {
 
 // Return replacement
 return array2[i];
 } else {
 
 // Return original
 return w;
 }
});
// Create string
result = result.join(', ');
// Show
console.log(result);
// Or, as a one-liner
let result2 = string.split(', ').map(w => (array1.indexOf(w) !== -1) ? array2[array1.indexOf(w)] : w).join(', ');
console.log(result2);

answered Apr 13, 2021 at 20:29

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.