1

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:

[
  '1:1',   'blah',
  '1:2',   undefined,
  '1:3',   'smith',
  '1:4',   'blah',
  '1:5',   'williams',
  '1:6',   'blah',
  '1:7',   'blah'
]

and I have another array that looks like this:

[ 
   'taylor', 
   'smith', 
   'williams', 
   'brown'
]

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:

[
  '1:1',   'johnson',
  '1:2',   'johnson',
  '1:3',   'smith',
  '1:4',   'johnson',
  '1:5',   'williams',
  '1:6',   'johnson',
  '1:7',   'johnson',
]
1
  • 2
    what have you tried so far? Commented Jan 19, 2021 at 20:23

3 Answers 3

1

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

var originalArray = [
  '1:1',   'blah',
  '1:2',   undefined,
  '1:3',   'smith',
  '1:4',   'blah',
  '1:5',   'williams',
  '1:6',   'blah',
  '1:7',   'blah'
];

var matchArray = [ 
   'taylor', 
   'smith', 
   'williams', 
   'brown'
];

for (var i = 0; i < originalArray.length; i++) {
    var value = originalArray[i];
    //Check if it matches your RegEx
    if (value !== undefined) {
      var doesItMatchRegEx = value.match(/([0-9]+):([0-9]+)/g);
    } else {
      originalArray[i] = "johnson";
    }
    //Check if it is in your second array
    var isItInSecondArray = matchArray.includes(value);
    if (!doesItMatchRegEx && !isItInSecondArray) {
        //Let's replace it with Johnson
        originalArray[i] = "johnson";
    }
}

console.log(originalArray);    
Sign up to request clarification or add additional context in comments.

5 Comments

I think you need to consider that the original array can contain undefined values and when matching a string to a regex you should use value.match instead of test.
Or you could just check if array value is not string and skip it.
There that should do it
var doesItMatchRegEx = value.test(/([0-9]+):([0-9]+)/g); TypeError: value.test is not a function
I accepted an edit, try it again and let us know if it works!
0

Let's call the main array arr and the array of names names, then this should do the trick:

  • arr.map(item => !/([0-9]+):([0-9]+)/g.test(item) && !names.includes(item) ? 'johnson' : item)

2 Comments

didn't work. everything still has it's original value
.map returns a copy, so you should replace the original one with it if you want to change the values of the original array, like so: arr = arr.map(item => !/([0-9]+):([0-9]+)/g.test(item) && !names.includes(item) ? 'johnson' : item)
0

I would recommend a functional approach using the map() function of the array. Alternatively could use a filter(), but since you want to replace certain entries the map does it with less steps.

The Array.prototype.map() method takes in a function that will be called on each item in the array, and the return of that function will go in that index of the return.

validNames = [‘taylor’...];
numberFormatRegex = /([0-9]+):([0-9]+)/g;

// This function will run on each value in the array, and the RETURN becomes the new value in the array created after mapping
mapFunction = (value) => {
  if(!validNames.includes(value) && !numberFormatRegex.test(value)){
    // only values NOT in the validNames list AND (&&) NOT passing the refer will enter this branch 
    
    return ‘Johnson’;
  }
  // This only runs if the previous return didn’t so it will only happen for valid values

}

// Call map() method with our new function and save result to new array
// note that the inputArray will be unchanged and a new one will be returned and assigned to newArray
newArray = inputArray.map(mapFunction);


Comments

Your Answer

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.