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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:55:05+00:00 2026-06-06T10:55:05+00:00

I’m trying to convert each integer in a string to its corresponding value in

  • 0

I’m trying to convert each integer in a string to its corresponding value in an object based on its key–value entries. For example if I have:

var arr = {
    "3": "value_three",
    "6": "value_six",
    "234": "other_value"
  };
var str = "I want value 3 here and value 234 here";

I would expext the output to be:

new_str = "I want value_three here and value other_value here"
  • 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-06T10:55:06+00:00Added an answer on June 6, 2026 at 10:55 am

    I’m just doing this off the top of my head, but this should work.

    var new_str = str;
    
    for (var key in arr) {
        if (!arr.hasOwnProperty(key)) {
            continue;
        }
    
        new_str = new_str.replace(key, arr[key]);
    }
    

    If you wanted all occurrences of the number to be replaced, you’d need to incorporate a Regex into the mix:

    var new_str = str;
    
    for (var key in arr) {
        if (!arr.hasOwnProperty(key)) {
            continue;
        }
    
        new_str = new_str.replace(new RegExp(key, "g"), arr[key]);
    }
    

    Also, I’d pick another name other than arr, as that implies it’s an array when it’s clearly an object. Also, make sure you only use for-in loops on objects, not arrays, because of issues with prototype leakage and others.

    You can also do this with jQuery, but it’s probably overkill:

    var new_str = str;
    
    $.each(arr, function (key, value) {
        new_str = new_str.replace(key, value);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I convert a character into an integer value? For example, I've been
Trying to convert a string of binary inputs into a vector of ints. I'd
Trying to convert int arrays to string arrays in numpy In [66]: a=array([0,33,4444522]) In
im trying to convert a date format into a new format in this example
I'm trying to convert this code from C# to VB.NET string[] lines = theText.Split(new
I am trying to convert an integer number in C into an array containing
I am writing this code to convert a hex entry into its integer equivalent.
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
string messagestatus; string.TryParse( Convert.ToString(DataBinder.Eval(e.Row.DataItem, Status)), out messagestatus ); I am trying to get the
I am trying to import a series of files from directory and convert each

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.