I am fairly new to Javascript and currently struggling with looping through an array and replacing items. I hope my explanation is clear.
Here is the initial array:
[
'1:1', 'blah',
'1:2', undefined,
'1:3', 'smith',
'1:4', 'blah',
'1:5', 'williams',
'1:6', 'blah',
'1:7', 'blah'
]
and here is another array:
[
'taylor',
'smith',
'williams',
'brown'
]
The goal is to replace any value in the first array that does not follow the format of /([0-9]+):([0-9]+)/g
and is not found in the second array. Therefore, all occurrences of "blah" and "undefined" in the first array should be replaced with johnson
. Any names that match the second array as well as the #:# numbers should remain unchanged. The desired output is:
[
'1:1', 'johnson',
'1:2', 'johnson',
'1:3', 'smith',
'1:4', 'johnson',
'1:5', 'williams',
'1:6', 'johnson',
'1:7', 'johnson',
]