1

I have an array of numbers such as [7,8,9,10]. I need each of the array elements as single digits to become 07,08,09 etc.

Any ideas?

ACesario
  • 89
  • 9

5 Answers5

2

You should try with something like this:

var data = [7, 8, 9, 10];

data = data.map(function(x) {
  return (x<10 && x>0) ? "0" + x : x;
});
console.log(data);
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
2

Simplest & the most basic way:

var index;
var a = [7, 8, 9,10];
for (index = 0; index < a.length; ++index) {
  if(a[index]>0 && a[index]<10)
    alert((0).toString()+a[index].toString());
  else
    alert(a[index])
}
Ani Menon
  • 27,209
  • 16
  • 105
  • 126
  • ani, is there any performance gain to this vs the one Starky posted above? – ACesario May 02 '16 at 19:07
  • Refer : [JQuery map vs Javascript map vs For-loop](http://stackoverflow.com/questions/6551139/jquery-map-vs-javascript-map-vs-for-loop) – Ani Menon May 03 '16 at 02:30
2
var numbers = [7,8,9,10];
function pad(n) {
return (n < 10) ? ("0" + n) : n;
}

for (i = 0; i < numbers.length; i++) {
    if(numbers[i] < 10){
     var n = numbers[i];
     pad(n);
    }
}

Have not tested this but its iterating through the array of numbers and adding a zero if the number is less than 10, what you do with the number afterwards is up to you, update the array value or push it into a new one.

1

You need to apply a zero padding on each element of your array. Just refer to this answer https://stackoverflow.com/a/1267338/1486897 and use the given function inside the Array.map. The resulting code will be something like:

// Here you should put the zeroFill function implementation

var data = [7, 8, 9, 10];

data = data.map(function(number) {
  return zeroFill(number, 2);
});

console.log(data);
Community
  • 1
  • 1
starky
  • 640
  • 5
  • 13
  • Thanks Starky. works for simple data... OutputData:07,08,09,10 And for longer array... Data: 02,00,01,12,02,16,04,00,02,00,00,00,00,00,00,00,00,12,02,08,05,18,09,18,09,00,00,02,00,06,03,16,05,00,00,00,00,.... – ACesario May 02 '16 at 19:02
0

To provide a more versatile solution, the following function takes an optional second argument allowing you to specify how long each string in the array should be. If it's not provided then it uses the length of the longest item and pads any others with leading zeros to match that length. It also ignores the minus symbol in negative numbers when calculating string lengths and allows you to manipulate negative numbers as you want (just edit the last conditional in the function) - I've got it wrapping negative numbers in parantheses.

var arr=[7,8,9,10];
pad(arr);
console.log(arr);//["07","08","09","10"]

arr=[7,8,-9,10]
pad(arr,3);
console.log(arr);//["007","008","(009)","010"]

function pad(array){
  var len,neg,x=1,
      max=arguments[1]||(function(){
        array.forEach(function(item){
          if((len=Math.abs(item).toString().length)>x)
            x=len;
        });
        return x;
      })();
  array.forEach(function(item,index){
    neg=item<0;
    len=(item=Math.abs(item).toString()).length;
    for(;len<max;len++)
      item="0"+item;
    if(neg)
      item="("+item+")";
    array[index]=item;
  });
};
Shaggy
  • 6,696
  • 2
  • 25
  • 45