简体   繁体   English

找到最大的数字并增加 1

[英]Find highest number and increment by 1

I have created a small script that creates a folder with a unique id, ie ABCD01.我创建了一个小脚本,它创建了一个具有唯一 ID 的文件夹,即 ABCD01。

The ABCD is based on the input name and the number is based on how many matches there are for the 4 digit code ie If have 3 folders all with the the code ABCD, the next number would be 04 as it's counted 3 matches already. ABCD 基于输入名称,数字基于 4 位代码的匹配数,即如果有 3 个文件夹都带有代码 ABCD,下一个数字将是 04,因为它已经被计算为 3 个匹配项。

This script works but I found an issue.该脚本有效,但我发现了一个问题。 If I have folders ABCD01, ABCD02, ABCD03 and I delete ABCD02, the next number would be ABCD03 as it can only count 2 instances of ABCD.如果我有文件夹 ABCD01、ABCD02、ABCD03 并且我删除了 ABCD02,下一个数字将是 ABCD03,因为它只能计算 2 个 ABCD 实例。 I now have 2 ABCD03's which will cause an issue.我现在有 2 个 ABCD03,这会导致问题。

Is it possible to take the highest number and increment its by 1?是否可以取最大的数字并将其增加 1?

Here's my code这是我的代码

console.log("there's no match");
let RemoveSpecials = BaseName.replace(/[^0-9a-zA-Z]/g, ""); // ABCCompanyThe
let FourDigitCode = RemoveSpecials.slice(0,4); // ABCC  
let UpperCaseCode = FourDigitCode.toUpperCase(); // ABCC  
let code = (' (') // (
let newcode = code.concat(UpperCaseCode); // (ABCC  
let CompiledName = BaseName.concat(newcode); // ABC Company, The (ABCC
console.log(CompiledName);

let list = ["ABC Company, The (ABCD01)"];
console.log(list);

let AlreadyExist = fmCloud.listContents(strPath).filter(hhh => hhh.includes(newcode)).length;
    this.number = this.number || AlreadyExist;
    this.number++;
    let Num = ('' + this.number);
    if(('' + Num).length == 1) {
        Num = '0' + Num;
    }
    let CompiledNamhghge = BaseName.concat(newcode + Num + ')'); // ABC Company, The (ABCC

I have tried using something like the following but doesn't work.我试过使用类似下面的东西但不起作用。

const foo = ["Company Name (COMP01)", "Company Name, The (COMP02)", "Computer Name, The (COMP03)"]

let lastNumber = Math.max(foo);
let nextNumber = parseInt(lastNumber)+1;
console.log (nextNumber)

 const foo = [ 'Company Name (COMP01)', 'Company Name, The (COMP02)', 'Computer Name, The (COMP03)' ]; const numFromString = str => str.match(/\d+/)[0]; let lastNumber = Math.max(...foo.map(f => numFromString(f))); let nextNumber = lastNumber + 1; console.log(nextNumber);

let lastNumber = Math.max.apply(null,foo.map(function(name){return Number(name.match(/(\d+)\)$/)[1]);}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM