Array(3)
ๅชๆ่จญๅฎ้ฃๅ้ทๅบฆ {length: 3}
๏ผไธฆไธๆ่จญๅฎใๆดๆธ็ดขๅผใๅฑฌๆง
ๅฆๆๅ .map()
๏ผๅชๆๅพๅฐ็ฉบ้ฃๅ๏ผๅ ็บ .map()
ๆไฟ็ใๆดใใ
( ๐ ไธ้ข็จๅผ็ฌฌ 15 ่ก )
Array(3).fill()
ๆๅกซๅ
ฅ undefined
๏ผไธฆ่จญๅฎใๆดๆธ็ดขๅผใๅฑฌๆง๏ผ้ๆไฝฟ็จ .map()
ๅฐฑๆๆๅฏฆ้ๆๆใ( ๐ ไธ้ข็จๅผ็ฌฌ 19 ่ก )
const { log } = console;
let n = 3;
// โญ๏ธ Array(n) does NOT set integer propertiesโ๏ธ
let emptyArray = Array(n); // only set {length: n}
log(Object.keys(emptyArray)); // []
// โญ๏ธ Array(n).fill() DOES set integer propertiesโ๏ธ
let filledArray = Array(n).fill(); // {0:undefined, 1:undefined, ...}
log(Object.keys(filledArray)); // [ '0', '1', '2' ]
// โญ map over INTEGER propertiesโ๏ธ๏ธ (โญ๏ธ no integer to map over)
let a1 = emptyArray.map((_, i) => i); // [ <3 empty items> ]
log(a1, a1.length); // โญ๏ธ `length` is kept by map
// โญ map over INTEGER propertiesโ
let a2 = filledArray.map((_, i) => i); // [ 0, 1, 2 ]
log(a2);
array.fill() - ๆฒๅกซๅฏซๅๆธ๏ผ่กจ็คบ filled with undefined