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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:14:49+00:00 2026-06-04T01:14:49+00:00

I’m looking for the most efficient way to rename (append-1, -2 etc.) a variable,

  • 0

I’m looking for the most efficient way to rename (append-1, -2 etc.) a variable, if it already exists in a string.

So I’m keeping an array”

dupeCheck = [];

And as soon as I see that a variable:

var UID;

Is already in my dupeCheck array, I want to immediately append the value of UID with -1,

Also, I need to prevent a third duplicate becoming string-1-1, but rather string-2..

I saw this: Appending the count to duplicates in a javascript string array before, but It’s nog exactly what I want…

Any smart ideas? I prefer jQuery..

/Edit:

For example:

var dupeUIDCheck = [];  

$.each(data[IDS].UIDs[keys], function(keys, val)
     {
     var currString = val;
     switch (key)
 {
      case "UID":

       UID = unquote(currString);

   //TODO:
   //Detect if multiple UIDs are loaded from a single source, and
   //rename them:

   dupeUIDCheck.push(UID); //Push current ID onto existing array

       //Check if ID exists
       ?
       //If exists rename value of currString, save it in currString
       newName = currSting;
      break;

      case "otherstuff":
           //Other vars to parse
      break;
     }

So when we get out of the “UID” case, I want to make sure it has a unique value

  • 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-04T01:14:50+00:00Added an answer on June 4, 2026 at 1:14 am

    The best way to keep a list of things you’re checking for dups on is to have them in an object, not an array so you can look them up quickly and then generate a unique suffix that isn’t already in use each time. This function allows you to pass an id into the function and have the function return a unique version of that id that isn’t already in use. If what was passed in was not in use, it just returns that. If what was passed in was in use, it strips any suffix off and generates a new suffix that isn’t already in use and returns the newly minted id. The newly minted id is then stored in the data structure so it won’t be duplicated in the future too.

    var idList = {};
    
    makeIdUnique(id) {
        if (id in idList) {
            // remove any existing suffix
            var base = id.replace(/-\d+$/, "");
            // generate a new suffix
            var cnt = idList[base] || 1;
            // while new suffix is in the list, keep making a different suffix
            do {
                id = base + "-" + cnt++;
            } while (id in idList);
            // save cnt for more efficient generation next time
            idList[base] = cnt;
        }
        // put the final id in the list so it won't get used again in the future
        idList[id] = true;
        // return the newly generated unique id
        return(id);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Specifically, suppose I start with the string string =hello \'i am \' me And
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

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.