Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

resize array javascript

// Quick way
a = ['A','B','C','D'];
console.log(a.length); // 4
a.length = 3;
console.log(a); // ['A', 'B', 'C'];

// Proper way
function resize(arr, newSize, defaultValue) {
    return [ ...arr, ...Array(Math.max(newSize - arr.length, 0)).fill(defaultValue)];
}
 
PREVIOUS NEXT
Tagged: #resize #array #javascript
ADD COMMENT
Topic
Name
9+3 =