In this code:
str.map(function(char) {
if (char.isPropertyOf(pairs)) {
char = pairs[char]
} )
}
Why cant you put in if statement in array map?
In this code:
str.map(function(char) {
if (char.isPropertyOf(pairs)) {
char = pairs[char]
} )
}
Why cant you put in if statement in array map?
arr.map(function(char) {
if (pairs.hasOwnProperty(char)) {
char = pairs[char]
} } )
I am trying to convert char to pairs[char] if char is in object pairs
I figured it out, thanks!!
I did
return arr.map(function(char) {
if (pairs.hasOwnProperty(char)) {
return char = pairs[char]
}
else {
return char = char
}
} )
Something like that. Ya I am returning the array right away and not using it later, that was one of the issues
I recommend for ... of
instead of forEach
as it works better in some scenarios. Like, async code. You can find details in the link below.
Here’s the same thing in one line using ternary operator (one-line if-else).
arr.map(char => pairs.hasOwnProperty(char) ? pairs[char] : char)
Also, char = char
assigns char
to itself, I’m not sure this is what you wanted…
return char = pairs[char]
will assign pairs[char]
to char
but that char
variable will be destroyed when you exit the function, so char =
part is redundant, you can simply use pairs[char]
Full code would be return pairs[char]
.
Hope it helps!