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
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.