Can't replace() character in Javascript

const locUnScr = [3, 5, 7];
const lowerTrimArr = ['Blaaaaaa_Beans', 'Trunked_Split', 'BoooooooooP_goat']

let camCase = [];

let mergedArr = [[...locUnScr], [...lowerTrimArr]];

for (const [i, d] of [mergedArr]) {
  camCase.push(d.replace('_', '*'));
}

console.log(camCase);

Error

Uncaught TypeError: d.replace is not a function
    at script.js

It's doing my nut in why isn't it replacing the _ with a * to all in the destructed array for var d?

Any help very welcome

Zigs

@ziggelflex , hi, where is the d object/class defined ?

I deconstructed the mergedArr arrays, i is defined as the first array and d as the second. So the d array has all the strings in and the i array has all the numbers in. Which you don’t need to worry about.

If you do this you’ll see:

for (const [i, d] of [mergedArr]) {
 console.log(i, d);
}
1 Like

if you check the object type you'll see its an array type.

console.log('Object.prototype.toString.call(d):', Object.prototype.toString.call(d));

if you iterate over each element of mergedArr[1] ... check out the following (for demo purpose only !!)

cat test.js 
const locUnScr = [3, 5, 7];
const lowerTrimArr = ['Blaaaaaa_Beans', 'Trunked_Split', 'BoooooooooP_goat']

let camCase = [];

let mergedArr = [[...locUnScr], [...lowerTrimArr]];

console.log('mergedArr[1]: <<',mergedArr[1], '>>');

for (let i = 0; i < mergedArr[1].length; i++) {
    let tmp=mergedArr[1][i];
    camCase.push(tmp.replace(/_/g, '*'));
}

console.log('camCase: <<',camCase, '>>');

# test it
node test.js 
mergedArr[1]: << [ 'Blaaaaaa_Beans', 'Trunked_Split', 'BoooooooooP_goat' ] >>
camCase: << [ 'Blaaaaaa*Beans', 'Trunked*Split', 'BoooooooooP*goat' ] >>

hopefully you'll see that it's the individual elements of mergedArr[1] that are strings whereas mergedArr[1] is an array , so string functions barf at that. :sunglasses:

for demonstration purposes:
updated version of your original as per:

  • added an extra element (integer 22) to the lowerTrimArr
  • added additional _ within 1 of the members
  • modified the loop to cater for arrays with non string members
  • used global regex syntax /_/g to replace all occurances within a member rather than '_' which will only do the first.
const locUnScr = [3, 5, 7];
const lowerTrimArr = ['Blaaaaaa_Beans', 'Trunked_Split', 'BoooooooooP_goat', 22 ]

let camCase = [];

let mergedArr = [locUnScr,lowerTrimArr];

for (const [i, d] of [mergedArr]) {
    if ( Array.isArray(d) )
        for ( member in d ){
            if ( typeof d[member] === 'string' )
                camCase.push(d[member].replace(/_/g, '*'));
            else
                camCase.push(d[member]);
        }
}

console.log(camCase)

Yo Bro,

You're great at this but I'm in Europe and it's late. I could stay up all night to find out but I'm afraid I'll have to get back to you tomoz. Second solution looks like the one. will test out first thing bro.

Night you digital ninjas!

Zigs :woozy_face:

1 Like

I'm 'in Europe' (but not the EU !) too :smiley:

1 Like

Hi, let us know how this goes for you please, if it solves your issue mark as complete/solution. post any other challenges/issues in new thread.

tks

Sorry for the late reply.

I'm getting this error:

Uncaught ReferenceError: member is not defined

can't work out why. I've read the code. I understand the code and yet it won't work. Seems like it should.

Zigs

then you're doing something (perhaps elsewhere in your code) 'wrong'

here's my copy/paste session

node
Welcome to Node.js v20.4.0.
Type ".help" for more information.
> const locUnScr = [3, 5, 7];
undefined
> const lowerTrimArr = ['Blaaaaaa_Beans', 'Trunked_Split', 'BoooooooooP_goat', 22 ]
undefined
> 
> let camCase = [];
undefined
> 
> let mergedArr = [locUnScr,lowerTrimArr];
undefined
> 
> for (const [i, d] of [mergedArr]) {
...     if ( Array.isArray(d) )
...         for ( member in d ){
...             if ( typeof d[member] === 'string' )
...                 camCase.push(d[member].replace(/_/g, '*'));
...             else
...                 camCase.push(d[member]);
...         }
... }
4
> 
> console.log(camCase)
[ 'Blaaaaaa*Beans', 'Trunked*Split', 'BoooooooooP*goat', 22 ]
undefined
> 

screenshot from one of the online js compilers ....

so, I'm reasonably confident the code posted works.

' ```
Uncaught ReferenceError: member is not defined

without context (ie the code running) is not particularly helpful at this point.
try the code posted in isolation , if it works, then something elsewhere in your environment/code is causing this - perhaps another js object with the same name ....

It works if you comment out 'use strict'; I rather it worked in strict mode.

I'm gonna try a few things I'll get back to you.

Ziggy :grinning:

'use strict';

const locUnScr = [3, 5, 7];
const lowerTrimArr = [
  'Blaaaaaa_Beans',
  'Trunked_Split',
  'BoooooooooP_goat',
  22,
];

let camCase = [];

let mergedArr = [locUnScr, lowerTrimArr];

for (const [i, d] of [mergedArr]) {
  if (Array.isArray(d))
    for (member in d) {
      if (typeof d[member] === 'string')
        camCase.push(d[member].replace(/_/g, '*'));
      else camCase.push(d[member]);
    }
}

console.log(camCase);

Exact code ...

you hadn't mentioned the use of strict previously, np, do this then .

for (const [i, d] of [mergedArr]) {
    let member = [];
    if ( Array.isArray(d) )
        for ( member in d ){
            if ( typeof d[member] === 'string' )
                camCase.push(d[member].replace(/_/g, '*'));
            else
                camCase.push(d[member]);
        }
}

Sorry my bad! Should have included that! :exploding_head:

Boom!

'use strict';

const locUnScr = [3, 5, 7];
const lowerTrimArr = [
  'Blaaaaaa_Beans',
  'Trunked_Split',
  'BoooooooooP_goat',
  22,
];

let camCase = [];

let mergedArr = [locUnScr, lowerTrimArr];

for (const [i, d] of [mergedArr]) {
  let member = [];
  if (Array.isArray(d))
    for (member in d) {
      if (typeof d[member] === 'string')
        camCase.push(d[member].replace(/_/g, '*'));
      else camCase.push(d[member]);
    }
}

console.log(camCase);

Thanks @munkeHoller

1 Like

Just a note.

I've found out you can now use replaceAll('_', '*') instead of replace(/_/g, '*')

Time are a changing! :crazy_face:

yep, i knew that, personal preference , i'm old && old school, i'm happy and with replace(....) :older_man: :sunglasses:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.