问题描述
我正在寻找最有效的方法来重命名(append-1,-2等)变量,如果它已经存在于字符串中。
所以我保留一个数组
dupeCheck = [];
pre>
只要我看到一个变量:
var UID;
已经在我的dupeCheck数组中,我想立即附加UID的值 - 1,
另外,我需要防止第三个重复变成字符串1-1,而是字符串2 ..
我看到了这一点:中复制,但它正好是我想要的...
任何聪明的想法?我喜欢jQuery ..
/编辑:
例如:
var dupeUIDCheck = [];
$ .each(data [IDS] .UIDs [key s],function(keys,val)
{
var currString = val;
switch(key)
{
caseUID:
UID = unquote(currString);
// TODO:
//检测是否从单个来源加载多个UID,
//重命名它们:
dupeUIDCheck.push (UID); //将当前ID推送到现有的数组
//检查ID是否存在
?
//如果存在currString的重命名值,将其保存在currString
newName = currSting;
break;
caseotherstuff:
//其他vars解析
break;
}
所以当我们摆脱UID的情况下,我想做确定它有一个独特的价值
解决方案保存一个你要检查的东西的列表的最好方法是拥有他们在一个对象,而不是一个数组,所以你可以快速查找它们,然后生成一个唯一的后缀,每次都没有使用。此功能允许您将id传递到函数中,并使该函数返回尚未使用的该id的唯一版本。如果传递的内容没有被使用,它只是返回。如果传入的内容正在使用中,则会剥离任何后缀,并生成一个尚未使用的新后缀,并返回新建的标识。然后将新创建的ID存储在数据结构中,以便将来也不会被复制。
var idList = {};
makeIdUnique(id){
if(id in idList){
//删除任何现有的后缀
var base = id.replace(/ - \d + $ /,);
//生成一个新的后缀
var cnt = idList [base] || 1;
//当新的后缀在列表中时,继续使用不同的后缀
do {
id = base + - + cnt ++;
} while(id in idList);
//保存cnt以便更有效地生成下一次
idList [base] = cnt;
}
//将最终的ID放在列表中,以便将来不会再被使用
idList [id] = true;
//返回新生成的唯一ID
return(id);
}
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.
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); }
这篇关于重命名Javascript数组中的重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!