How can I return an array that maps some filtered elements while keeping the non-filtered elements in their original positions? Is there a straightforward way to achieve this?
array
.filter(
function(element){
// some test
}
)
.map(
function(element){
// some mapping
}
)
The solution I've come up with so far looks something like this:
array
.map(
function(value, index){
if (<test>) {
return <mapping>(value);
}
}
)
However, I feel that this approach somewhat goes against the principles of functional programming.
I'm not specifying a particular language implementation here, but an example in Scala or JavaScript would be appreciated.
EDIT: Here's a specific example of what I'm aiming for:
[1,2,3,4,11,12]
If we map all elements to element*10, and only apply this transformation to elements greater than 10, the resulting array should look like:
[1,2,3,4,110,120]
EDIT2: I apologize for using the term "mutate." My intention was not to mutate the original array - rather, I meant mutating a copy of the array.