Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6657205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:44:19+00:00 2026-05-26T01:44:19+00:00

I want to return an array that that maps some filtered elements – but

  • 0

I want to return an array that that maps some filtered elements – but I want to keep the non-filtered elements where they are.
i.e. Is there an easy way to do this?:

array
.filter(
  function(element){
    // some test
  }
)
.map(
  function(element){
    // some mapping
  }
)

The closest solution I’ve come up with is something along the lines of:

array
.map(
  function(value, index){
    if (<test>) {
      return <mapping>(value);
    }
  }
)

but I feel this somewhat breaks the spirit of functional programming.

I’m not asking for a specific language implementation, although an example in Scala or JavaScript would be nice.

EDIT: Here’s a concrete example of what I’m looking for:

[1,2,3,4,11,12]

Mapping all elements to element*10, for all elements in array which are greater than 10, should yield

[1,2,3,4,110,120]

EDIT2: I apologize for using the word “mutate.” I did not mean mutate the original array – I was thinking more along the lines of mutating a copy of the array.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:44:20+00:00Added an answer on May 26, 2026 at 1:44 am

    It’s not really going to be functional if you’re using a mutable collection. But you can use transform in Scala:

    scala> val a = Array(1,2,3,4,11,12)
    a: Array[Int] = Array(1, 2, 3, 4, 11, 12)
    
    scala> a.transform {i => if(i > 10) i * 10 else i}
    res10: scala.collection.mutable.WrappedArray[Int] = WrappedArray(1, 2, 3, 4, 110, 120)
    

    edit:
    If you want filter and map separated, use a view:

    scala> a
    res22: Array[Int] = Array(1, 2, 3, 4, 11, 12)
    
    scala> a.view.filter(_ > 10).transform(_ * 10)
    res23: scala.collection.mutable.IndexedSeqView[Int,Array[Int]] = SeqViewF(...)
    
    scala> a
    res24: Array[Int] = Array(1, 2, 3, 4, 110, 120)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I want to return an immutable array like this + (NSArray *)ids but
i ve created two dimensional array inside a function, i want to return that
I have an array that I want to permutate randomly. In Java, there is
I want a simple function that receives a string and returns an array of
I want to return a private field from a remoted object but i get
I want to return a set of values from function till the point they
I want to return some errors to my jquery method. What is happening is
I have a 2D-array that I want to sort based on the second column.
Lets say we have a grid containing cells, and that we want to return
I have a string that I want to compare against an array of strings,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.