- Array.prototype.slice.call(類數(shù)組對(duì)象)
例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(類數(shù)組對(duì)象)
例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"]
- 擴(kuò)展運(yùn)算符
例1: function foo(a, b) {
let res = [...arguments];
console.log(res);
}
foo('aaa', 'bbb'); //(2) ["aaa", "bbb"]
注:擴(kuò)展運(yùn)算符不能將json格式的類數(shù)組對(duì)象轉(zhuǎn)化成數(shù)組;肌3楦摺!
let json = {
0: 'aaa',
1: 'bbb',
2: 'ccc',
length: 3
}
let res = [...json];
console.log(res); Uncaught TypeError: json is not iterable at 類數(shù)組轉(zhuǎn)數(shù)組.html:22