Skip to content
Advertisement

Replace an item in an array that doesn’t match any item in another array with a specific value

I’m still VERY new to Javascript and I’m having trouble with looping through an array and replacing items. I hope this explanation is clear.

I have an array that looks like this:

JavaScript

and I have another array that looks like this:

JavaScript

I want to replace any value in the first Array that isn’t in a /([0-9]+):([0-9]+)/g format and isn’t found in the second array. So all the “blah” and “undefined” in the first Array should be replaced with johnson but the names that match the second Array and the #:# numbers still remain, so the output shows:

JavaScript

Advertisement

Answer

We can use a simple if statement inside a for loop to achieve what you are looking for.

JavaScript
Advertisement