0

I want to iterate an array of strings and replace each element with another string:

let arr = ["-","-","-"]

let spaceArr = replaceStringArr.forEach(replaceDash)

const replaceDash = function(index:any, array:any){
   // replace the value of each element containing "-" with "&nbsp"
   I have tried 
   return array[index] = "&nbsp"
   and
   array[index] = "&nbsp"
}

I have tried:

const replaceDash = function(index: any, array: any) {
  return array[index] = "&nbsp"
}

as well as:

const replaceDash = function(index: any, array: any) {
  array[index] = "&nbsp"
}

But I am getting a:

TypeError: Cannot create property '-' on number '0'

  • 2
    `function(index:any, array:any)` is the wrong function definition for `forEach`. It's actually `function(element: any, index: number, array: any[])`. Or probably better with generics: `function(element: T. index: number, array: T[])`. So your code right now is trying to assign some properties to a number (second argument is always the index). – VLAZ Apr 20 '21 at 19:46
  • `const replaceDash = function(element:any, index: number, array:any[]){ return array[index] = "&nbsp" }` – Christian Cuneo Apr 20 '21 at 19:51
  • 1
    But `forEach` doesn't return anything anyways – Nadia Chibrikova Apr 20 '21 at 19:54
  • Does this answer your question? [For-each over an array in JavaScript](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) – ponury-kostek Apr 20 '21 at 19:59

2 Answers2

1

forEach receives the parameter currentValue first, index second, array third. You would need to modify the function as follows:

const replaceDash = function(item:any, index:any, array:any){
   // replace the value of each element containing "-" with "&nbsp"
   if(item === "-") array[index] = "&nbsp";
}

If I were you I'd also take a look at map function, which will give you a new array and seems to be more suited to what you want in less code:

let spaceArr = replaceStringArr.map(item => item === '-' ? '&nbsp' : item);
Fabio Lopez
  • 517
  • 2
  • 5
  • 15
-1

It seems you are misunderstanding for .forEach works and confusing it for .map.

.forEach just iterates through an array, it doesn't return anything, while .map actually returns the new modified array. If you want spaceArr to be the array with modified values, try this:

let arr = ["-","-","-"]

let spaceArr = arr.map(value => {return "&nbsp"})
Pitis
  • 173
  • 5
  • "*but when you are calling it, it has no parameters.*" [What is the difference between a function call and function reference?](https://stackoverflow.com/q/15886272) – VLAZ Apr 20 '21 at 19:59
  • But OP is *not* calling the function - it's being passed as a reference to `.forEach` and that is entirely correct. Aside from the function definition being wrong and the operations inside, that is but the call `replaceStringArr.forEach(replaceDash)` is 1. syntactically correct 2. has no missing arguments. – VLAZ Apr 20 '21 at 20:08
  • then who is `replaceStringArr`? do you know it is an array? it doesn't even appear in the code, maybe it is a number – Pitis Apr 20 '21 at 20:13
  • If it isn't an array (or anything with a `.forEach()` method that takes a callback) then it's a) quite exceptional b) there would be a different error. As for what it is - I don't know. It's a prudent guess that it *is* an array, given that 1. it has `Arr` in the name 2. has a `.forEach()` method. 3. said method seems to accept a callback. 4. there is no error indicating the method call or the parameter supplied is wrong. With that said, I don't know what OP wants to do or what the actual code is. I've voted to close for lack of clarity. – VLAZ Apr 20 '21 at 20:17