首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Javascript -如何通过添加数字来使记录具有唯一性?

Javascript -如何通过添加数字来使记录具有唯一性?
EN

Stack Overflow用户
提问于 2020-08-01 19:33:57
回答 2查看 135关注 0票数 0

现在,当文件已经存在时,我为文件名添加了前缀,这是一个时间戳,以使其唯一。

但我不想使用时间戳,而是使用序号后缀或在文件名中添加一个数字。

如果文件存在,我会在文件名中添加一个递增的数字。但我不能完全理解如何以一种好的方式做到这一点。

使用时间戳是可行的,但是它太长了,例如,当我们显示文件名时,它就像这样,所以我不使用时间戳,而是只想将一个数字递增到一个文件名。

Hellworldfilename - 1593024232 - timestamp太长了,不是一个好主意。

它应该基于数据库中的现有记录。例如,如果我添加了一个文件名为Hellworldfilename的文件并且它已经存在,那么新的文件名将是Hellworldfilename-1,如果我再次添加Hellworldfilename,则新的文件名将是Hellworldfilename-2,依此类推。你知道怎么让文件名每次都是唯一的吗?

让我举个例子。假设我在数据库中有3个文件名为filesname

代码语言:javascript
复制
DOC
DOC-1
DOC-2

如果我添加一个文件名为DOC的文件,新的文件名现在将是DOC-3。

#检查文件是否存在的代码

代码语言:javascript
复制
const file = await context.service.Model.findOne({
    where: { humanId: record.id, filename: data.filename },
    paranoid: false,
  });

if (file) {
    const prefix = Date.now().toString();
    // eslint-disable-next-line no-undef
    const fileParts = data.filename.split('.');
    filename = `${fileParts[0]}-${prefix}.${fileParts[1]}`;
EN

回答 2

Stack Overflow用户

发布于 2020-08-01 20:09:01

您可以在全局范围内声明一个对象filenames,如下所示

代码语言:javascript
复制
const filenames={};

并使用它来跟踪已经打开的文件。

下面我定义了一个函数makeUnique(),用来照亮我之前提到的想法。原来我不得不稍微调整一下代码,但下面是一个有效的代码片段:

代码语言:javascript
复制
const makeUnique=(function(){
 const filenames={};
 return function(fn){
  const fileParts=fn.split(".");
  const prefix=filenames[fn]!=null
  ? ++filenames[fn]
  : filenames[fn]=0;
  if (prefix) fileParts[Math.max(fileParts.length-2,0)]+='-'+prefix;
  return fileParts.join('.')
 }
})();

console.log(["abc","d.e.f.c","abc","ghi","abc","abc.txt","def",
             "abc.txt","d.e.f.c","abc.txt","abc"].map(makeUnique))
代码语言:javascript
复制
.as-console-wrapper {max-height:100% !important}

我使用一个生命为静态对象filenames生成了一个受保护的作用域。这现在可以由makeUnique()的所有调用来访问,但除此之外是“私有的”,即不能从其他任何地方意外地修改。

票数 0
EN

Stack Overflow用户

发布于 2020-08-01 20:28:10

您需要检查文件名是否以-somenumber结尾。如果是,那么您可以提取该数字并递增它。否则,将1放入结果中:

代码语言:javascript
复制
function getNumberedFileName(fileN) {
    //These are value initializations to cope with the situation when the file does not have a .
    var fileName = fileN;
    var fileExtension = "";
    var lastDotIndex = fileN.lastIndexOf(".");
    if ((lastDotIndex > 0) && (lastDotIndex < fileN.length - 1)) { //We are not interested in file extensions for files without an extension hidden in UNIX systems, like .gitignore and we are not interested in file extensions if the file ends with a dot
        fileName = fileN.substring(0, lastDotIndex);
        fileExtension = "." + fileN.substring(lastDotIndex + 1);
    }
    var lastDashIndex = fileName.lastIndexOf("-");
    if ((lastDashIndex > 0) && (lastDashIndex < fileName.length - 1)) {
        var lastPart = fileName.substring(lastDashIndex + 1);
        if (!isNaN(lastPart)) {
            var index = parseInt(lastPart) + 1;
            return fileName.substring(0, lastDashIndex) + "-" + index + fileExtension;
        }
    }
    return fileName + "-1" + fileExtension;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63204745

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档