阅读:3451回复:3
关于new Array().fill()的用法
当我们想借助new Array()生成指定数组长度的假数据的时候,我们可以这样写:
const data = new Array(5).map((v, i) => { return { name: "XXX" + i, price: i, category: Math.random() > 0.5 ? '蔬菜' : '水果' } }) console.log(data) // (5) [empty × 5] 以下便是关于new Array().fill的用法了 const data = new Array(5).fill().map((v, i) => { return { name: "XXX" + i, price: i, category: Math.random() > 0.5 ? '蔬菜' : '水果' } }) console.log(data) // (5) [{…}, {…}, {…}, {…}, {…}] //0: {name: "XXX0", price: 0, category: "蔬菜"} //1: {name: "XXX1", price: 1, category: "蔬菜"} //2: {name: "XXX2", price: 2, category: "蔬菜"} //3: {name: "XXX3", price: 3, category: "水果"} //4: {name: "XXX4", price: 4, category: "水果"} //length: 5 //__proto__: Array(0) |
|