什么是类数据?
JavaScript对它们定义为:它们看起来很像数组,只是具有部分和数组相同特性:
①拥有length属性;
②元素保存在对象中,可以通过索引访问但是没有数组的其他方法,例如:push、slice、indexOf等;
JS类数组转成数组的方法?
①Array.prototype.slice.call(arguments)
②Array.from(arguments)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
/*
什么是类数据?
JavaScript对它们定义为:它们看起来很像数组,只是具有部分和数组相同特性:
①拥有length属性;
②元素保存在对象中,可以通过索引访问但是没有数组的其他方法,例如:push、slice、indexOf等;
JS类数组转成数组的方法
①Array.prototype.slice.call(arguments)
②Array.from(arguments)
*/
var obj = {
"0": 'A',
"1": "B",
"2": "c",
"length": 3,
"push": Array.prototype.push
}
//①Array.prototype.slice.call(arguments)
console.log(Array.prototype.slice.call(obj))
//②Array.from(arguments)
console.log(Array.from(obj))
</script>
</html>