Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shortcut to create padded array in JavaScript?

I have this javascript:

function padded_array(k, value){
    var a = [];
    a[k] = value;
    return a;
}

padded_array(3, "hello"); //=> [undefined, undefined, undefined, 'hello']

Is it possible to shorten the code in the function body?

like image 345
maček Avatar asked Dec 06 '11 19:12

maček


People also ask

How do I create an array filled in JavaScript?

In JavaScript, you can use the Array. fill() method to populate an array with a zero or any other value like an object or a string. This method replaces all elements in an array with the value you want to populate the array with and returns the modified array.

What are the three ways to construct array in JavaScript?

Create an array This example shows three ways to create new array: first using array literal notation, then using the Array() constructor, and finally using String.prototype.split() to build the array from a string.

How do you fill an array?

Using the fill() method The fill() method, fills the elements of an array with a static value from the specified start position to the specified end position. If no start or end positions are specified, the whole array is filled. One thing to keep in mind is that this method modifies the original/given array.

How do you fill an array with 0's?

Use the fill() method to create an array filled with zeros, e.g. new Array(3). fill(0) , creates an array containing 3 elements with the value of 0 . The fill() method sets the elements in an array to the provided value and returns the modified array.


2 Answers

for all the googlers coming here - you're probably looking for this:

var pad_array = function(arr,len,fill) {
  return arr.concat(Array(len).fill(fill)).slice(0,len);
}
like image 93
k0m0r Avatar answered Sep 20 '22 01:09

k0m0r


From 2020 & 2021 : straight forward options

Let assume that is your Array

const yourArray = [1,2]

If you just want to loop 4 times (maybe for react jsx )

Array.from({length:4}) //[undefined,undefined,undefined,undefined]
Array(4).fill()//[undefined,undefined,undefined,undefined]

If you want to loop yourArray 4 times, but to start with values you already have

// unmutation option
Array.from({...yourArray, length:4}) //[1,2,undefined,undefined]

// unmutation option, but need some calcualtion
[...yourArray , ...Array(2)  ] //[1,2,undefined,undefined]
[...Array(2), ...yourArray   ] //[undefined,undefined,1,2]

// loop on your array several times 
Array(3).fill(yourArray).flat() // [1, 2, 1, 2, 1, 2]

// mutation the original array.
yourArray.length = 4;
Array.from(yourArray) //[1,2,undefined,undefined]

If You actually want an Array with full of values. ex. with increment numbers.
Remap it

// unmutation option

Array.from({...yourArray,length:4}, (v,i) => v ?? i+1 ) 
// [1,'2',3, 4]

// Or, mutation the original array. and fill with "x"
array.yourArray.length = 4;

Array.from(yourArray, (v) => v ?? 'x') 
// [1,'2','x','x'] 
like image 40
pery mimon Avatar answered Sep 19 '22 01:09

pery mimon