簡體   English   中英

找到最大的數字並增加 1

[英]Find highest number and increment by 1

我創建了一個小腳本,它創建了一個具有唯一 ID 的文件夾,即 ABCD01。

ABCD 基於輸入名稱,數字基於 4 位代碼的匹配數,即如果有 3 個文件夾都帶有代碼 ABCD,下一個數字將是 04,因為它已經被計算為 3 個匹配項。

該腳本有效,但我發現了一個問題。 如果我有文件夾 ABCD01、ABCD02、ABCD03 並且我刪除了 ABCD02,下一個數字將是 ABCD03,因為它只能計算 2 個 ABCD 實例。 我現在有 2 個 ABCD03,這會導致問題。

是否可以取最大的數字並將其增加 1?

這是我的代碼

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

我試過使用類似下面的東西但不起作用。

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