1.array.from:2.展开运算符:3.array.prototype.slice.call()
判断数组类型:
①最好用array.isarray()其余判断方式有时会报错
Array.isArray() 用于检查一个值是否为数组类型。它会返回一个布尔值。
Array.isArray([]); // true
Array.isArray({}); // false
②typeof :
typeof 42; // "number"
typeof 'Hello'; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof null; // "object"(这是一个历史遗留问题,实际上 null 是一个特殊的对象类型)
typeof []; // "object"(数组被视为对象)
typeof {} // "object"
typeof function() {} // "function"
③instanceof 用于检查一个对象是否属于某个特定的对象类型。
let arr = [];
let obj = {};
console.log(arr instanceof Array); // true
console.log(obj instanceof Object); // true
④Object.prototype.toString.call(); 可以返回一个表示对象类型的字符串。
Object.prototype.toString.call(42); // "[object Number]"
Object.prototype.toString.call('Hello'); // "[object String]"
Object.prototype.toString.call(true); // "[object Boolean]"
Object.prototype.toString.call([]); // "[object Array]"
Object.prototype.toString.call({}); // "[object Object]"
Object.prototype.toString.call(function() {}); // "[object Function]"