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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:02:00+00:00 2026-05-30T07:02:00+00:00

I am working on a function that receives an object organised in this manner:

  • 0

I am working on a function that receives an object organised in this manner:
{“oldID1″:”newID1”, “oldID2″:”newID2” , … }

This information is to be fed into another function which will perform a string replace of oldID1 with newID1, oldID2 with newID2 etc, inside a textarea containing these ID’s.

This initial object originally comes from a series of sorting operations, such that the object keys represent a list’s original order, and the object values represent the newly sorted order. So, for example, I would be confronted with an object looking like this:

{
"1":"3",
"2":"1",
"3":"2",
"4":"4"
}

Where the intent was to move item 3 to the top of the list. As a result, for the text in the separate textarea, 1 should be replaced by 3, 2 by 1 and 3 by 2.

When I try to iterate the object and perform the replace, I run into problems because some text gets replaced more than once. So ‘1’ gets replaced by ‘3’, but then later on gets replaced by ‘2’.

Here is the code I’m using to loop through that object:

$.each(the_object, function(key, value){   
      replace_text(key,value);
});

function replace_text(old_id, new_id){
    var textarea        = $("#the_textarea");
    var str             = textarea.val();
    var replace_regex   = new RegExp("<pic_"+old_id+">");
    str = str.replace(replace_regex, '<pic_'+new_id+'>');
    the_textarea.val(str);
}

I should emphasise that the sorting process is independent of the text replacing. I am simply concerned in finding the best way to perform that text replace given the object I have.

Thanks in advance

  • 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-30T07:02:03+00:00Added an answer on May 30, 2026 at 7:02 am

    Given you object containing the IDs are named idObj and it is available in the closure of the function passed to replace, I guess this should work:

    var textarea = $('#the_textarea');
    textarea.val(function(i,str){
      return str.replace(/<pic_(.*?)>/g,
         function(m,n){ 
           return '<pic_' + (idObj[n] || n) + '>';
         });
    });
    

    The n variable here will contain the old index. I see that the properties of idObj is named after the old indices, thus the new index can be retrieved like this var newIndex = idObj['oldIndex]; and the function returns for any given old index.

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

Sidebar

Related Questions

I am working on a helper macro that look into the list function on
I was just working on a function that I needed to return two values,
I search for the function that run programm by path, and working of main
Hey, I was working with a PHP function that takes multiple arguments and formats
In the application I'm working on, there is a function that connects with a
Does anyone have a working class or function to create the hashed email that
I am working with a unit-testing suite that hijacks function calls and tests expected
Working in Delphi7 just now, I noticed that not only a VarIsEmpty function exists,
I'm working on a upload script and using move_uploaded_file() function. The problem is, that
Is piping parameter into line is working only for functions that accept one parameter?

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.