当前位置:网站首页>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);
边栏推荐
- ImportError: libGL.so.1: cannot open shared object file: No such file or directory
- 相对路径和绝对路径
- Customer Rating Control
- 微信小程序云开发如何将页面生成为pdf?
- Deveco studio Hongmeng app access network detailed process (js)
- 【装机】老毛桃的安装及使用
- npm and package.json
- 区间问题 : 今年暑假不AC
- debian 10 nat and routing forwarding
- js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
猜你喜欢

排序学习笔记(二)堆排序

三元判断再三元判断

解决MySQL创建子视图并查看的时候,字符集报错问题

DSPE-PEG-Silane, DSPE-PEG-SIL, phospholipid-polyethylene glycol-silane modified active group

js基础知识

Cut out web icons through PS 2021

PCL—point cloud data segmentation

C语言的变长数组

Error in render: “TypeError: Cannot read properties of null (reading ‘0‘)“ 报错解决方案

DOM manipulation---magnifying glass case
随机推荐
SOCKS5
5.20今日学习
require modular syntax
微信小程序云开发之券码领取,怎么防止用户领取到相同的数据?
require模块化语法
querystring模块
oracle inner join and outer join
Amazon sellers how to improve the conversion
getattr() function analysis
sh: 1: curl: not found
广州华为面试总结
display,visibility,opacity
mysql阶段总结
每日面试题 2022/7/28
page load process
【装机】老毛桃的安装及使用
Questions about your resume
The usage of json type field in mysql
JJWT tool class
Phospholipid-polyethylene glycol-targeted neovascularization targeting peptide APRPG, DSPE-PEG-APRPG