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
  • 15,996
  • 17
  • 81
  • 153

5 Answers5

10

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
  • 7,629
  • 1
  • 21
  • 43
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
  • 7,819
  • 3
  • 20
  • 47
  • 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
  • 715
  • 2
  • 8
  • 26
0

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

guzmanoj
  • 2,370
  • 23
  • 31
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
  • 19,906
  • 6
  • 35
  • 55