- Array.prototype.slice.call(类数组对象)
例1: function foo(a, b) {
let res = Array.prototype.slice.call(arguments);
console.log(res);
}
foo('aaa', 'bbb'); //(2) ["aaa", "bbb"]
例2: let json = {
0: 'aaa',
1: 'bbb',
2: 'ccc',
length: 3
}
let res = Array.prototype.slice.call(json);
console.log(res); //(3) ["aaa", "bbb", "ccc"]
- Array.from(类数组对象)
例1:function foo(a, b) {
let res = Array.from(arguments);
console.log(res);
}
foo('aaa', 'bbb'); //(2) ["aaa", "bbb"]
例2: let json = {
0: 'aaa',
1: 'bbb',
2: 'ccc',
length: 3
}
let res = Array.from(json);
console.log(res); //(3) ["aaa", "bbb", "ccc"]
- 扩展运算符
例1: function foo(a, b) {
let res = [...arguments];
console.log(res);
}
foo('aaa', 'bbb'); //(2) ["aaa", "bbb"]
注:扩展运算符不能将json格式的类数组对象转化成数组!!!
let json = {
0: 'aaa',
1: 'bbb',
2: 'ccc',
length: 3
}
let res = [...json];
console.log(res); Uncaught TypeError: json is not iterable at 类数组转数组.html:22