9

I want my array to have minimum of n length.

If the number of elements is below the given length then I want it to be filled with given/default element until length is n.

It's very much like String padEnd function.

a=[1,2,3]
a.padEnd(6, null);
a;// [1,2,3,null,null,null];

my solution so far:

n = 10, value = {};
arr.concat(Array(n).fill(value, 0, n)).slice(0, n);
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168

6 Answers6

11

There is no native function to do a padEnd on Array in JavaScript. So I advice to use Object.assign to do it:

const a = [1, 2, 3];
console.log(Object.assign(new Array(5), a));
// Outputs [1, 2, 3, undefined, undefined]

We can wrap it in a function, it would be more readable. We can also choose the fill value as a optional parameter:

function padEnd(array, minLength, fillValue = undefined) {
    return Object.assign(new Array(minLength).fill(fillValue), array);
}

const a = [1, 2, 3] 
console.log(padEnd(a, 6));
// Outputs [1, 2, 3, undefined, undefined, undefined]

console.log(padEnd(a, 6, null));
// Outputs [1, 2, 3, null, null, null]
tzi
  • 8,719
  • 2
  • 25
  • 45
2

Unfortunately there's no native function for this. But you can always do it by yourself:

const a = [1,2,3];
a.concat(Array(6 - a.length).fill(null));

You can create a function following the above approach and make it part part of your own Array helpers, but as a good practice of JS, refrain from modifying the prototype of native objects:

Don’t modify objects you have no control over.

Finally, regarding the docs, here you go: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

WSD
  • 3,243
  • 26
  • 38
2

I'm not sure if there is a native function to do so but you could create your own like so.

This function will create an array of the length of the remaining element to be added and set their value to null in this case. It will then append the values to the current array.

let originalArray = [1,2,3];

Object.defineProperty(Array.prototype, "padEnd", {
    enumerable: false,
    writable: true,
    value: function(value, number) {
      this.push(...new Array(number - this.length).fill(value));
      return this;
    }
});

console.log(originalArray.padEnd(null, 6));

If you wish to have more information on the defineProperty function, check out bergi's answer here

Nicolas
  • 8,077
  • 4
  • 21
  • 51
  • 1
    @T.J.Crowder Thank you for pointing it out, i had no idea i could do that. – Nicolas Jan 27 '20 at 17:39
  • Just FWIW, normally you'd have `configurable: true` as well (and `false` is the default for all three flags, so you *could* leave `enumerable: false` off; I often include it for emphasis depending on the target audience). – T.J. Crowder Jan 27 '20 at 18:20
1

There are lots of answers here, but none of them mention what seems like the most obvious way of doing this.

while(myArray.length < desiredLength) myArray.push(fillValue)

So for your example:

a=[1,2,3]
while(a.length < 6) a.push(null)

I believe this method is easiest to read also.

Paul Wieland
  • 765
  • 2
  • 10
  • 29
0

There is not such method for arrays so far but you can add like this

Array.prototype.padEnd = function (number, value) {
  if (this.length >= number) return this;
  const values = new Array(number - this.length).fill(value);
  this.push.apply(this, values);
  return this;
}

let a = [1,2,3]
a.padEnd(6, null);

console.log(a);

let b = [1,2,3,4,5,6,7];

b.padEnd(6, null);
console.log(b);
Zohaib Ijaz
  • 21,926
  • 7
  • 38
  • 60
0

Note that a lot of the solutions posted here and in other S/O answers do not take into account that the desired padded length may be lower than the actual array length, and would reduce the length of your array. That may be an erroneous condition, or just a no-op.

Also, if you want an in-place solution, you can also simply do:

// pad array of length 3 up to 10
let arr = [1, 2, 3];
arr.length = 10;
arr.fill(0, 3, 10);

Or, as a (typescript) method with a length check:

function padArray<T>(arr: T[], padValue: T, maxLength: number) {
    const currentLength = arr.length;
    if(currentLength >= maxLength) {
        return arr;
    }

    arr.length = maxLength;
    arr.fill(padValue, currentLength, maxLength);

    // return array to enable chaining
    return arr;
}


let arr = [1, 2, 3];
padArray(arr, 0, 2);
Philipp Sumi
  • 917
  • 8
  • 20