Answers for "resize array javascript"

0

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)];
}
Posted by: Guest on July-04-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language