最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Is there padEnd for Arrays in Javascript? - Stack Overflow

programmeradmin6浏览0评论

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);

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);
Share Improve this question edited Jan 27, 2020 at 17:39 Muhammad Umer asked Jan 27, 2020 at 17:18 Muhammad UmerMuhammad Umer 18.1k24 gold badges109 silver badges174 bronze badges 5
  • have you read the documentation? – Daniel A. White Commented Jan 27, 2020 at 17:20
  • How about setting your array to a size and then fill()ing it. console.log(Array(6).fill("n")); – zero298 Commented Jan 27, 2020 at 17:21
  • this wont work with the example I have, it'd override existing elements – Muhammad Umer Commented Jan 27, 2020 at 17:22
  • @DanielA.White yes, there isn't as far I could see. – Muhammad Umer Commented Jan 27, 2020 at 17:23
  • then you have your answer.... – Daniel A. White Commented Jan 27, 2020 at 17:24
Add a comment  | 

6 Answers 6

Reset to default 12

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]

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

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

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.

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);

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);
发布评论

评论列表(0)

  1. 暂无评论