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 8057139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:55:47+00:00 2026-06-05T08:55:47+00:00

I was wondering how to convert an array to an object by splitting the

  • 0

I was wondering how to convert an array to an object by splitting the values ?

var obj = {
    id: '',
    val: ''
}

By this I mean – if I have an array like

["abc12", "abc1", "def12", "abc454"]

How I could split the first 3 values off – so it end up like an object like:

{id: 'abc', val: 12}, {id: 'abc', val: 1} ...
  • 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-05T08:55:49+00:00Added an answer on June 5, 2026 at 8:55 am

    One solution is to map your array (like herby noted) with a function that converts an element of your array to an object of your desired form.
    In this case, the id is represented by all the characters before the first digit and the val represents the digits from the back of the string :

    source.map(function(x) {
        return { 
            id: x.replace(/(^.*?)\d+$/,'$1'), 
            val: parseInt(x.replace(/^.*?(\d+)$/,'$1'),10) 
        };
    });
    

    Here’s a working demo

    Ps: You may want to be careful and check if the map method exists in the Array prototype chain, because older browsers may not have this implemented. Here’s a link that explains how to cover this browser incompatibility : http://www.tutorialspoint.com/javascript/array_map.htm.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering how can convert CGcolorRef to UIColor ? I have created a
I was wondering if there was a tool to convert a path data like
I have an array of objects and I want to convert it to an
I have a list of 700 words that I would like to convert to
I'm wondering how to query a database using an array, like so: $query =
I was wondering how to convert a file I have on my server (pdf/excel/ppt)
I wanted to convert an array to lowercase and was wondering the most efficient
Hey, I'm just wondering how to convert a numerical date into text format in
I was wondering if it is possible to convert a wpf application to silverlight
I am wondering how exactly I can convert a location (X Y Z) to

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.