Thursday, May 23, 2024

JavaScript: Array.from()

 Array.from() - JavaScript | MDN


How to initialize an array's length in JavaScript? - Stack Overflow

Array.from('abcde'),

Array.from('x'.repeat(5))

Array.from({length: 5}, (v, i) => i) // gives [0, 1, 2, 3, 4]

const randomNumbers = Array.from({length: 10}, () => Math.floor(Math.random() * 100));

function getRandomColor() {
  var r = Math.floor(Math.random() * 256);
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);
  return 'rgba(' + r + ', ' + g + ', ' + b + ', 0.2)';
}

const randomColors = Array.from({length: 10}, () => getRandomColor());

No comments: