本文介绍了在Javascript阵列重命名重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找最有效的方式来重命名(追加-1,-2等)的变量,如果它已经在一个字符串存在。

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

所以,我保持一个数组

dupeCheck = [];

和,只要我看到一个变量:

And as soon as I see that a variable:

var UID;

已经在我的dupeCheck阵列,我要立即用-1追加UID的值,

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

另外,我需要prevent第三重复成为字符串1-1,而是串2 ..

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

我看到这一点:Appending 之前在JavaScript字符串数组的计数的重复,但它的NOG正是我想要的......

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

任何智能的想法?我preFER jQuery的..

Any smart ideas? I prefer 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的情况下,我想确保它有一个独特的价值

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

推荐答案

要保持你检查上DUP的事情的清单,最好的办法是让他们在一个对象,而不是一个数组,所以你可以查找。很快,然后生成一个唯一的后缀,每一次,是不是已经在使用。此功能可以让你一个id传递到功能,具备的功能返回的ID尚未使用的唯一版本。如果什么传入没在使用,它只是返回。如果什么传递是在使用中,它去掉任何后缀关闭并生成新的后缀,是不是已经在使用并返回新崛起的ID。然后新崛起ID存储在数据结构中,所以它不会在未来被复制过

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阵列重命名重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:47