auto increment string in javascript 1

auto increment string in javascript

function autoIncrementCustomId(lastRecordId){
  let increasedNum = Number(lastRecordId.replace('ABC','')) + 1;
  let kmsStr = lastRecordId.substr(0,3);
  for(let i=0; i< 6 - increasedNum.toString().length; i++){
    kmsStr = kmsStr+'0';
  kmsStr = kmsStr + increasedNum.toString();


Here is what the above code is Doing:
1. It takes the last record id and removes the first 3 characters from it.
2. It then converts the remaining string to a number and adds 1 to it.
3. It then adds the first 3 characters back to the number.
4. It then adds 0s to the number to make it 6 digits long.
5. It then converts the number back to a string and returns it.

Similar Posts