美文网首页前端基础学习
数组类型检测与转换

数组类型检测与转换

作者: 小雪洁 | 来源:发表于2020-03-16 14:09 被阅读0次
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>类型检测与转换</title>
    </head>
    <body>
        <div>hxj</div>
        <div>ydc</div>
        
        <script>
            //类型检测
            const array=[1,2,3];
            console.log(Array.isArray(array));
            //数组转字符串
            /* console.log(array.toString());//1,2,3
            console.log(array.join("-"));//1-2-3
            console.log(String(array));//1,2,3
            const str=array.join("-");
            console.log(location.href+"?id="+str); */
            //字符串转数组
            const hxj="haoxuejie";
            console.log(hxj.split());//["haoxuejie"]
            console.log(hxj.split(""));// ["h", "a", "o", "x", "u", "e", "j", "i", "e"]
            const hy="hxj,ydc";
            console.log(hy.split(","));//["haoxuejie", "yangdingchuan"];
            //Array.from(str)可以将字符串转成数组,因为字符串有length
            console.log(Array.from(hy));//["h", "x", "j", ",", "y", "d", "c"]
            const obj={
                name:"hhh",
                age:"30"
            };
            console.log(Array.from(obj));//[]
            const o={
                0:"hhh",
                1:"30",
                length:2
            }
            console.log(Array.from(o));//["hhh", "30"]
            let divs=document.querySelectorAll("div");
            console.log(divs);//NodeList(2) [div, div]
            //Array.from()可以转换成数组,还可以对数组元素进行一定的处理
            console.log(Array.from(divs));// [div, div]
            Array.from(divs,function(item){
                console.log(item.innerHTML);
                item.style.background="pink";
                return item;
            });//hxj   ydc
        </script>
    </body>
</html>

相关文章

  • 数组类型检测与转换

  • javascript的数组Array

    创建数组及修改 类型检测 转换方法 Array栈操作 Array队列操作 Array的反转和排序 数组的连接与截取...

  • js 数组的类型检测与转换

    Array.isArray() **用于确定传递的值是否是一个 [Array] Array.from()方法就是将...

  • JavaScript进阶部分

    引用类型 object类型可以用typeof来判断某个属性是否存在 Array类型 1)检测数组 2)转换方法 3...

  • Java 类型转换

    引用对象数组类型转换 String数组转Integer数组 注:支持数组与数组之间的直接转换,如String->L...

  • 类型检测与类型转换:"is" 与 "as"

    "is" 与 "as" "is" 操作符 代表对象是否是该指定类型"!is" 操作符 代表对象是否不是该指定的类...

  • 009——基础加强

    检测变量 打印变量 类型转换 销毁变量 函数 时间戳函数 读取文件夹 数组 static 类

  • java基础

    数组与List之间的转换 数组—>List 引用类型数组可以直接使用 Arrays.asList() String...

  • js数组的操作方法

    检测数组方法 isArray()判断是否是数组 转换方法 toString()数组转换字符串方法数组的 value...

  • JavaScript中的原型链与函数等问题

    数据类型 隐式转换 变量转换为字符串、===、==(类型不同,尝试转换类型再比较) 类型检测 typeof NaN...

网友评论

    本文标题:数组类型检测与转换

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