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

  • SEARCH
  • Home
  • 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 8616079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:32:01+00:00 2026-06-12T05:32:01+00:00

Part of a web app I’m working on handles forms that need to be

  • 0

Part of a web app I’m working on handles forms that need to be bound to a collection of model (case class) instances. See this question

So, if I were to add several users at one time, form fields would be named email[0], email[1], password[0], password[1], etc.

Posting the form results in a Map[String, Seq[String]]

Now, what I would like to do is to process the Map in batches, by index, so that for each iteration I can bind a User instance, creating a List[User] as the final result of the bindings.

The hacked approach I’m thinking of is to regex match against "[\d]" in the Map keys and then find the highest index via filter or count; with that, then (0..n).toList map{ ?? } through the number of form field rows, calling the binding/validation method (which also takes a Map[String, Seq[String]]) accordingly.

What is a concise way to achieve this?

  • 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-06-12T05:32:02+00:00Added an answer on June 12, 2026 at 5:32 am

    Assuming that:

    1. All map keys are in form "field[index]"
    2. There is only one value in Seq for each key.
    3. If there is entry for "email[x]" than there is entry for "password[x]" and vice versa.

    I would done something like this:

    val request = Map(
      "email[0]" -> Seq("alice@example.com"),
      "email[1]" -> Seq("bob@example.com"),
      "password[0]" -> Seq("%vT*n7#4"),
      "password[1]" -> Seq("Bfts7B&^")
    )
    
    case class User(email: String, password: String)
    
    val Field = """(.+)\[(\d+)\]""".r
    
    val userList = request.groupBy { case (Field(_, idx), _) => idx.toInt }
                          .mapValues { userMap =>
                              def extractField(name: String) =
                                  userMap.collect{case (Field(`name`, _), values) => values.head}.head
                              User(extractField("email"), extractField("password"))}    
                          .toList.sortBy(_._1).map(_._2)
    
    // Exiting paste mode, now interpreting.
    
    request: scala.collection.immutable.Map[String,Seq[String]] = Map(email[0] -> List(alice@example.com), 
    email[1] -> List(bob@example.com), password[0] -> List(%vT*n7#4), password[1] -> List(Bfts7B&^))
    defined class User
    Field: scala.util.matching.Regex = (.+)\[(\d+)\]
    userList: List[User] = List(User(alice@example.com,%vT*n7#4), User(bob@example.com,Bfts7B&^))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Part of my app caches web pages for offline viewing. To do that, I
I'm working in a web app framework, and part of it consists of a
I am developing a web app that will be working with other companies web
Part of our web app has a little Ajax method that will load a
I have a new web app that is packaged as a WAR as part
As part of my web app, I built a system that periodically pulls an
I'm a new Rails programmer working on a web app. As part of this
I have a web app that needs both functionality and performance tested, and part
In my (PHP) web app, I have a part of my site that keeps
I have a python daemon running as a part of my web app/ How

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.