什么是类数组(伪数组),如何将其转化为真实的数组?
伪数组
1、具有 length 属性
2、按索引方式存储数据
3、不具有数组的 push.pop 等方法
伪数组(类数组):无法直接调用数组方法或期望 length 属性有什么特殊的行为,不具有数组的 push.pop 等方法,但仍可以对真正数据遍历方法来遍历它们。典型的是函数
document.childnodes 之类的,它们返回的 nodeList 对象都属于伪数组
伪数组-->真实数组
1.使用 Arrray.from()--ES6
2.[].slice.call(eleArr) 或则 Array.prototype.slice.call(eleArr)
示例:
let eleArr = document.querySelectorAll('li');
Array.from(eleArr).forEach(function(item){
alert(item);
});
let eleArr = document.querySelectorAll('li');
[].slice.call(eleArr).forEach(function(item){
alert(item);
})
浙公网安备 33010602011771号