美文网首页
将可遍历类型转成数组

将可遍历类型转成数组

作者: shirleyyu001 | 来源:发表于2017-05-26 23:02 被阅读0次
1、使用扩展运算符(...)
let a=document.querySelectAll('div'),
    b=[...a];
console.log(a.constructor===NodeList); //true
console.log(a.constructor===Array); //true
备注:鉴于浏览器的兼容性,此方法需要babel转码。
2、Array.prototype.slice.call
var dom=document.querySelectorAll('div'),
    arr=Array.prototype.slice.call(dom);
console.log(arr.constructor===Array);  //true
3、使用for...of的方式
var dom=document.querySelectorAll('div'),
    arr=[];
for(var key of dom){
    arr.push(dom[key]);
}
console.log(arr.constructor===Array);  //true
备注:鉴于浏览器的兼容性,此方法需要babel转码。
4、将字符串转成字符数组
var a='asdgghhkfdld';
var b=a.split('');
console.log(b.constructor===Array);  //true

若需要去除字符串内的重复字符,可使用Set+...方法。

相关文章

  • 将可遍历类型转成数组

    1、使用扩展运算符(...) 备注:鉴于浏览器的兼容性,此方法需要babel转码。 2、Array.prototy...

  • 2019-10-17 Array.from

    Array.from 将可遍历的数据类型 或者 类数组的对象 转化为数组;

  • Swift小知识点

    逆序遍历数组 把字符或者字符串转成整型

  • ES6 Array

    类数组、可遍历 对象(arguments selector返回的对象)转成数组 Array.from() Arra...

  • Java中数组的常用操作

    目录: 1,声明数组;2,初始化数组;3,查看数组长度;4,遍历数组;5,int数组转成string数组;6,从a...

  • 数组

    数组定义: 元素类型 数组操作 Range 数组的批量操作 遍历

  • 数组

    数组的遍历 数组是值类型 数组的排序 冒泡排序 多维数组

  • 遍历数组和对象2018-08-14

    js数组遍历和对象遍历 针对js各种遍历作一个总结分析,从类型用处:分数组遍历和对象遍历;还有性能,优缺点等。 J...

  • 数组2

    js数组遍历和对象遍历 针对js各种遍历作一个总结分析,从类型用处:分数组遍历和对象遍历;还有性能,优缺点等。 J...

  • iOS字符串和char类型数组之间的转换

    NSString转char类型数组 char数组转NSString,这里就直接遍历拼接

网友评论

      本文标题:将可遍历类型转成数组

      本文链接:https://www.haomeiwen.com/subject/ihhffxtx.html