当前位置:网站首页>js的“类数组”及“类数组转数组”
js的“类数组”及“类数组转数组”
2022-08-02 03:23:00 【星雨668】
一、什么是类数组?
类数组:不是数组,能够通过下标访问到属性,并且有length属性;
所以类数组必须有以下几个组成部分:
- 属性要为“索引(数字)属性”;
- 必须有length 属性
例如:
let obj = {
"0" : 'a',
"1" : 'b',
"2" : 'c',
length : 3
}
常见的类数组有:
- 函数里的参数对象arguments;
- 用getElementsByTagName/ClassName/Name获得的HTMLCollection;
- 用querySelector获得的 NodeList;
二、常用类数组转数组方法
1、es6语法 Array.form(arr)
let obj = {
"0" : 'a',
"1" : 'b',
"2" : 'c',
length : 3
}
Array.form(obj) // ['a','b','c']
2、Array.prototype.slice.call(obj)或者 [].slice.call(obj)
let obj = {
"0" : 'a',
"1" : 'b',
"2" : 'c',
length : 3
}
Array.prototype.slice.call(obj) // ['a', 'b', 'c']
[].slice.call(obj) // ['a', 'b', 'c']
3、es6扩展运算符
function test(){
return [...arguments]; // [1,2,4]
}
test(1,2,4);
边栏推荐
- IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boo
- require模块化语法
- 4.14到新公司的一天
- 每日面试题 2022/7/28
- C语言入门小游戏—三子棋
- Scientific research reagent DMPE-PEG-Mal dimyristoylphosphatidylethanolamine-polyethylene glycol-maleimide
- SSM integration
- pyppeteer使用样例脚本
- 新工程加载YOLOV6的预训练权重问题
- kettle 安装与配置
猜你喜欢
随机推荐
客户评分控件
L1-039 古风排版(C)
SSM integration
解决glob()返回文件排序不一致问题&onnx本地按照安装方法
JJWT tool class
我的小笔记 =》其他东东
canvas--饼状图
最新,每天填坑,Jeston TX1 精卫填坑,第一步:刷机
docker 安装 sqlserver中的坑点
PCL—point cloud data segmentation
Chemical reagent Phospholipid-polyethylene glycol-hydroxyl, DSPE-PEG-OH, DSPE-PEG-Hydroxyl, MW: 5000
微信小程序怎么批量生成带参数的小程序码?
debian 10 nat and routing forwarding
Phospholipid-Polyethylene Glycol-Aldehyde DSPE-PEG-Aldehyde DSPE-PEG-CHO MW: 5000
AttributeError: Can't get attribute 'SPPF' on
URL模块
由中序遍历和后序遍历得到前序遍历(树的遍历)
Redis simple study notes
1.8今日学习
4.14到新公司的一天









