I am faced with an array containing a long list of Strings, and my goal is to filter out all the strings that begin with 'INSERT ALL' and replace the number inside the parentheses with the string ' NULL'
Here is the original array:
let arrSt = [ 'INSERT ALL bmw model 320d (111, bmw)', 'ford focus st d (222, ford)', 'INSERT ALL audi a5 rs (333, audi)', 'opel astra d (444, opel)', ]
This is the desired modified array:
[ 'INSERT ALL bmw model 320d (NULL, bmw)', 'ford focus st d (222, ford)', 'INSERT ALL audi a5 rs (NULL, audi)', 'opel astra d (444, opel)', ]
This is the approach I took:
arrSt.filter((items: any) => items.startsWith('INSERT ALL')) .map((item: any) => item.replace(/\(\d+,/g, '(NULL,'))
However, despite using the .map method, the modifications do not seem to persist when checking the result using console.log(arrSt)
. Why does this method not affect the array as intended?
UPDATE:
In order to modify elements in an array of strings based on specific criteria, such as identifying those starting with a particular prefix like 'INSERT ALL', I need to implement a solution that caters to such requirements.
For example, given the array ['bmw', 'ferrari']
, I should only modify strings that start with 'INSERT ALL bmw' or 'INSERT ALL ferrari'. This ensures that only relevant records are updated while leaving out others that do not meet the specified conditions.