<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
/**
* 怎么把伪数组转换成真数组?
* 1.伪数组的特点:
* 1.1 key值是索引
* 1.2 有length属性
* 2.Array.from应用场景:
* 2.1 伪数组转真数组
* 2.2 加工数组(要用到第二个参数,如 Array.from(arr, x => x * 2),意思是将arr的每一项*2后加入新数组)
* 2.3 Array.from('abc') //['a','b','c']
*/
// ES6之后
let array = {0: "晨晨", 1: "蛋蛋", length: 2}
let newArr = Array.from(array)
console.log(newArr instanceof Array) // true
// ES6之前
let newArr1 = Array.prototype.slice.call(array)
console.log(Array.isArray(newArr1))
</script>
</body>
</html>
slice的使用和真伪数组的转换
最新推荐文章于 2024-08-04 16:33:59 发布