1
\$\begingroup\$

I have this number returned from an Ajax request: 015-0011-00001. I want to increment the last part of the number (00001) on document load. What I did was get the last number and increment it then combine the numbers again. Is there a cleaner way of incrementing this number without slicing the number into many parts?

DEMO

var data = [{"code":"456100010011","incrementingnumber":"015-0011-00001"}];

var code = data[0].code;
var inc = data[0].incrementingnumber;
var res = code.slice(-4);
var inc1 = inc.slice(-4);
//alert(inc1);
var inc2 = parseInt(inc1) + 1;

var incrementvalue = ("00000" + inc2).slice(-5); // -> result: "0001"

console.log("015-" + res + "-" + incrementvalue);
\$\endgroup\$
1
\$\begingroup\$

var data = [{"code":"456100010011","incrementingnumber":"015-0011-00001"}];

var code = data[0].code,
    inc = data[0].incrementingnumber;
var inc1 = inc.split('-');
inc1[2] = ('00000' + ++inc1[2]).slice(-5);

document.write(inc1.join('-'));

You don't need the code.slice, but you'd still need to split your incrementingnumber value.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Let me ask one thing when you join the segments the previous value of element in index 2 will be replace automatically when you declare that in line segments[2] = ("00000" + (+segments[2] + 1)).slice(-5); . I just want to understand what happened. thank you for making cleaner than before. \$\endgroup\$ – guradio Sep 22 '15 at 2:29
  • \$\begingroup\$ ++inc1[2] will increment the value at the 3rd index of inc1 array. Since .split splits the string to an array; you have inc1 = [[0] = '015', [1] = '0011', [2] = '00001']. On increment, the preceeding 0s get removed as the tpe changes from string to number. To format it back with 0-padding; we use slice. You can also use .substring instead of slice. \$\endgroup\$ – hjpotter92 Sep 22 '15 at 4:44
1
\$\begingroup\$

Here's another approach

var data = [{"code":"456100010011","incrementingnumber":"015-0011-00001"}];

var segments = data[0].incrementingnumber.split('-');
segments[2] = ("00000" + (+segments[2] + 1)).slice(-5);

document.write(segments.join('-'));

With very little context to go by, the last 4 digits of code appear to be the same as the second segment of incrementingnumber. I could very well assume they're the same, thus omitting code from being sliced.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Let me ask one thing when you join the segments the previous value of element in index 2 will be replace automatically when you declare that in line segments[2] = ("00000" + (+segments[2] + 1)).slice(-5); . I just want to understand what happened. thank you for making cleaner than before. \$\endgroup\$ – guradio Sep 22 '15 at 2:29
  • \$\begingroup\$ @Pekka Yes. The result of ("00000" + (+segments[2] + 1)).slice(-5) will be assigned to segments[2]. \$\endgroup\$ – Joseph Sep 22 '15 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.