When you do Id= "0" + Id;
, you're just updating the parameter value. That parameter value isn't saved anywhere when you're done.
If you want to update the values in the array, either assign back to the existing array or (more commonly) create a new array via map
:
myArray = myArray.map(Id => {
while (Id.length < 10) {
Id = "0" + Id;
}
return Id;
});
Or these days, you could use padStart
instead of the while
loop:
myArray = myArray.map(Id => Id.padStart(10, "0"));
Here's an example of assigning back to the original array, in case you wanted to do that:
myArray.forEach((Id, index) => myArray[index] = Id.padStart(10, "0"));
Live Examples:
// Creating a new array (1)
let myArray1 = ["1234567890", "0123456789", "12345678"];
myArray1 = myArray1.map(Id => {
while (Id.length < 10) {
Id = "0" + Id;
}
return Id;
});
console.log(myArray1);
// Creating a new array (2)
let myArray2 = ["1234567890", "0123456789", "12345678"];
myArray2 = myArray2.map(Id => Id.padStart(10, "0"));
console.log(myArray2);
// Updating the existing array
let myArray3 = ["1234567890", "0123456789", "12345678"];
myArray3.forEach((Id, index) => myArray3[index] = Id.padStart(10, "0"));
console.log(myArray3);