伪数组:个人观点是只要有length属性的对象
实例
<ul>
<li>111</li>
<li>222</li>
<li>333</li>
<li>444</li>
</ul>
伪数组转真数组的几种方法
1.扩展数组
2.Array.from
3.[].slice.call()
// 伪数组转真数组
{
let ali = document.querySelectorAll('ul li');
console.log(ali); //结果是伪数组 NodeList(4) [li, li, li, li]
let arrli = [...ali]; //扩展数组
let arrli2 = Array.from(ali);
console.log(arrli2)
let arrli3 = [].slice.call(ali); //ES5常用方法
console.log(arrli3)
arrli.pop(); //删除一项值
console.log(arrli) //[li, li, li]
}
例子1
{
function show() {
console.log(arguments) //[1, 2, 3, 4, 5, callee: ƒ, Symbol(Symbol.iterator): ƒ] 伪数组
console.log([...arguments]) //[1, 2, 3, 4, 5]
}
show(1, 2, 3, 4, 5)
}
例子2------字符串转换成数组
{
//字符串转换成数组
let str = 'string';
let arr = str.split('');
let arr1 = Array.from(str);
console.log(arr) //["s", "t", "r", "i", "n", "g"]
console.log(arr1) //["s", "t", "r", "i", "n", "g"]
}
例子3------伪数组对象转成数组
{
let json = {
0: '111',
1: '222',
2: '333',
length: 2
}
let arr = Array.from(json)
console.log(arr) //["111", "222"]
}