一、new Array()
(1) 【0个参数】代表:空数组
var a = new Array();
console.log( a ) // 相当于 []空数组
(2) 【1个参数】代表:指定了“数组长度”----【注意: 这是一个缺点】
- 解决办法:请查看Array.of() *
var a = new Array(10);
console.log( a ) // a.length 是 10
(3) 【1个参数】代表:指定了“数组长度”
var a = new Array(10, 11);
console.log( a ) // [10, 11]
二、Array.of()
// 0个参数
var a1 = Array.of();
console.log( a1 ) // 相当于 []空数组
// 1个参数: 这个和new Array() “唯一的区别”【重要重要】
var a2 = Array.of(10);
console.log( a2 ) // [10]
// 2/多个参数
var a3 = Array.of(10, 11);
console.log( a3 ) // [10, 11]
三、Array.from()
Array.from(object, mapFunction, thisValue)
3个参数
| 参数 | 描述 |
|---|---|
| object | 【必需】。这是一个“可迭代对象”或“类数组对象”, 通过Array.from可以转换为“数组”(注意:这是新数组)。 |
| mapFunction | 【可选】。对数组的每个项目调用的 map 函数。 |
| thisValue | 【可选】。执行 mapFunction 时用作 this 的值。 |
let arr1 = Array.from("ABCDEFG");
console.log(arr1) // ['A', 'B', 'C', 'D', 'E', 'F', 'G']
const mapFunction = item => {
return `${item}函数`
}
let arr2 = Array.from("ABCDEFG", mapFunction, /* thisValue【可选】 */ );
console.log(arr2); // ['A函数', 'B函数', 'C函数', 'D函数', 'E函数', 'F函数', 'G函数']