我正在寻找一种最有效的方法来重命名(append-1,-2等)一个变量,如果它已经存在于一个字符串中。
所以我保留了一个数组“
dupeCheck = [];
一旦我看到变量:
var UID;
已经在我的dupeCheck数组中,我想立即用-1附加UID的值,
另外,我需要防止第三个副本变成string-1-1,而不是string-2 ..
我之前看过这个:Appending the count to duplicates in a javascript string array,但它正是我想要的......
任何聪明的想法?我更喜欢jQuery ..
/编辑:
例如:
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;
}
因此,当我们退出“UID”案例时,我想确保它具有唯一值
答案 0 :(得分:6)
您可以将功能包装在函数中以便能够重用它。下面的函数采用字符串列表并返回-1
,-2
等后缀字符串列表。
function suffixDuplicates( list )
{
// Containers
var count = { };
var firstOccurences = { };
// Loop through the list
var item, itemCount;
for( var i = 0, c = list.length; i < c; i ++ )
{
item = list[ i ];
itemCount = count[ item ];
itemCount = count[ item ] = ( itemCount == null ? 1 : itemCount + 1 );
if( itemCount == 2 )
list[ firstOccurences[ item ] ] = list[ firstOccurences[ item ] ] + "-1";
if( count[ item ] > 1 )
list[ i ] = list[ i ] + "-" + count[ item ]
else
firstOccurences[ item ] = i;
}
// Return
return list;
}
例如,输入
[ "Barry", "Henk", "Jaap", "Peter", "Jaap", "Jaap", "Peter", "Henk", "Adam" ]
返回
的输出[ "Barry", "Henk-1", "Jaap-1", "Peter-1", "Jaap-2", "Jaap-3", "Peter-2", "Henk-2", "Adam" ]
要查看它的实际效果,here是指向jsFiddle示例的链接。
答案 1 :(得分:1)
保留要检查重复项的列表的最佳方法是将它们放在对象中,而不是数组中,以便您可以快速查找它们,然后生成一个尚未使用的唯一后缀每一次。此函数允许您将id传递给函数,并让函数返回尚未使用的该id的唯一版本。如果传入的内容未被使用,则只返回该内容。如果传入的内容正在使用中,它会删除任何后缀并生成一个尚未使用的新后缀并返回新创建的id。然后,新创建的id将存储在数据结构中,以便将来不会重复。
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);
}
答案 2 :(得分:1)
理解你的问题有点困难,但如果你指的是这个,请告诉我。假设我们有一串单词,有些单词重复。我们希望使用新的后缀修改这些重复的字词,例如-1
或-2
,具体取决于它的实例。
// Start by creating our string, word array, and result array
var string = "one two one one two one three three one three",
values = string.split(" "), result = [];
// For every word in the values array
for ( var i = 0; i < values.length; i++ ) {
// Set a word variable, and an integer suffix
var word = values[i], int = 1;
// While our current word (with/without suffix) exists in results
while ( strArr(word, result) >= 0 )
// Increment the suffix on our word
word = values[i] + "-" + int++;
// Push word into result array
result.push(word);
}
// Function for determining if a string is in an array
function strArr(s,a){
for ( var j = 0; j < a.length; j++ )
if ( a[ j ] == s ) return j;
return -1;
}
// Compare the before and after
console.log( string );
console.log( result.join(" ") );
我们的结果是
one two one one two one three three one three
one two one-1 one-2 two-1 one-3 three three-1 one-4 three-2